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.
- this is a JSP problem, not a JS problem.dandavis– dandavis2014-09-23 18:12:34 +00:00CommentedSep 23, 2014 at 18:12
3 Answers3
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 useencodeURIComponent:
document.location.href = 'display.jsp?img='+encodeURIComponent(uri);2 Comments
jsp code? You shouldask a new question, post the HTTP request and your serverside code there.It is not correct to useencodeURI() as this function encodes special character except:, / ? : @ & = + $ #
UseencodeURIComponent() to encode these characters.
For more info check below link:
Comments
Your problem is that theencodeURI 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 useencodeURLComponent.
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?
Comments
Explore related questions
See similar questions with these tags.