0

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.

1
  • this is a JSP problem, not a JS problem. Commented Sep 23, 2014 at 18:12

3 Answers 3

1

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);
Sign up to request clarification or add additional context in comments.

2 Comments

display.jsp page landing as empty page. why
Maybe there's a mistake in the jsp code? You should ask a new question, post the HTTP request and your serverside code there.
0

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

Comments

0

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?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.