Reputation: 79
I'm unable to encoding data URI:
var uri ="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQo...";
var res = encodeURI(uri);
document.location.href = 'display.jsp?img='+res;
After encoding, I'm getting the same uri. display.jsp
is landing as am empty page.
Upvotes: 0
Views: 93
Reputation: 1198
Your problem is that the encodeURI
function is for making a URL valid for a browser, not for formatting content into a URL (which is what you're doing). A base64 string is already formatted in such a way that it registers as valid. To encode it as part of the URL, you need to use encodeURLComponent
.
Basically, just use:
var uri ="data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgMCAgMDAwMEAwMEBQgFBQQEBQo...";
var res = encodeURIComponent(uri);
document.location.href = 'display.jsp?img='+res;
For more info, check out: When are you supposed to use escape instead of encodeURI / encodeURIComponent?
Upvotes: 0
Reputation: 862
It is not correct to use encodeURI()
as this function encodes special character except: , / ? : @ & = + $ #
Use encodeURIComponent()
to encode these characters.
For more info check below link:
http://www.w3schools.com/jsref/jsref_encodeuri.asp
Upvotes: 0
Reputation: 665475
There is no encoding happening because what you have there is already a valid, completely encoded URI.
If you want to use that as a parameter in an other URI, you should use encodeURIComponent
:
document.location.href = 'display.jsp?img='+encodeURIComponent(uri);
Upvotes: 1