views:

29

answers:

2

I am trying to grab a reference to images with src's in URI scheme. An example would be the images on google.com/news.

if I alert(escape(saveObj.image)); I get something like below:

data%3Aimage/jpeg%3Bbase64%2C/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCABQAFADASIAAhEBAxEB/8QAHAAAAQUBAQEAAAAAAAAAAAAABgIDBAUHAQAI/8QAPhAAAgECBAMFBgIGCwEAAAAAAQIDBBEABRIhEzFhBkFRcYEUIjKRobFCwRUjJFKC0QcWJSZiY3Jzg7Lw4f/EABoBAAIDAQEAAAAAAAAAAAAAAAMEAAIFBgH/xAAmEQABBAEEAQMFAAAAAAAAAAABAAIDESEEEjFBBRMisVFhcZGh/9oADAMBAAIRAxEAPwAr7L5pD2gyY5JXEtLGAFY/EU2sR1U2+nXF/pZFKuffViGPW5ximQUEz1cNdPNKms6g8TlWBufDcHyxsdLUmqoYqhiWZ1BYtsSe+/

I pass that from the js file and am using django to get that into a mysql table of type utf8_unicode_ci using modelform.save, but when i examine what's in the database, I see:

data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAgGBgcGBQgHBwcJCQgKDBQNDAsLDBkSEw8UHRofHh0aHBwgJC4nICIsIxwcKDcpLDAxNDQ0Hyc5PTgyPC4zNDL/2wBDAQkJCQwLDBgNDRgyIRwhMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjIyMjL/wAARCABQAFADASIAAhEBAxEB/8QAHAAAAQUBAQEAAAAAAAAAAAAABgIDBAUHAQAI/8QAPhAAAgECBAMFBgIGCwEAAAAAAQIDBBEABRIhEzFhBkFRcYEUIjKRobFCwRUjJFKC0QcWJSZiY3Jzg7Lw4f/EABoBAAIDAQEAAAAAAAAAAAAAAAMEAAIFBgH/xAAmEQABBAEEAQMFAAAAAAAAAAABAAIDESEEEjFBBRMisVFhcZGh/9oADAMBAAIRAxEAPwAr7L5pD2gyY5JXEtLGAFY/EU2sR1U2 nXF/pZFKuffViGPW5ximQUEz1cNdPNKms6g8TlWBufDcHyxsdLUmqoYqhiWZ1BYtsSe 

The key difference is that in my database all of the '+' characters from the original have been stripped and replaced with spaces. Any ideas? I'm going blind trying to figure this out! :P

The only javascript I am executing on the src before passing along to webservice via xmlhttprequest (POST, webservice, TRUE) is:

escape(image) where image = src of a google news image.
+2  A: 

The problem is that Django URLFields are automatically passed on to the urlsplit method and that removes stuff like this since a + also means space in urls.

As you can see here, the validation of the URLField automatically does this. So you can either use a CharField instead or create a custom URLField like this:

class CustomURLField(forms.URLField):
    def to_python(self, value):
        return value

Do note that it won't automatically add http anymore if you do that.

WoLpH
Thanks for the quick response. I am actually using:img_link = models.TextField(null=True, blank=True)because i am not sure how large the base64 img links get (they seem huge). using models.TextField, is there a similar constraint? I can't seem to find a reason why that would be the case.
citizencane
@citizencane: what does your form look like? I'm guessing that some escaping is incorrect somewhere. Also, do note that `escape()` is not the same as `encodeURI()`.
WoLpH
for that field the form isclass Copies(models.Model): img_link = models.CharField(null = True, blank=True, max_length = 3500)
citizencane
@citizencane: I've tried it locally and I can't reproduce it at all so I'm guessing that your javascript is escaping this incorrectly. Can you add the relevant javascript/html parts to your question?
WoLpH
strange the only JS i am executing is escape(image), (also in the edit above) where image is the src of a google news image URI. Alerting this shows the + characters. I send this to the webservice via xmlhttprequest('post',webserviceurl,true)
citizencane
That's your problem right there I'm guessing, try `encodeURI()` instead of `escape()`
WoLpH
thanks! You led me down the right path. it was encodeURIComponent(image) in Javascript.
citizencane
A: 

Since + in a URL means space, the spaces are fine, just so long as you encode the URL again before you query the server for it. It looks like it's also removing the trailing slash.

Marcus Adams