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.

Celeo's user avatar
Celeo
5,7138 gold badges41 silver badges44 bronze badges
askedSep 23, 2014 at 17:56
user3201607's user avatar
1
  • this is a JSP problem, not a JS problem.CommentedSep 23, 2014 at 18:12

3 Answers3

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 useencodeURIComponent:

document.location.href = 'display.jsp?img='+encodeURIComponent(uri);
answeredSep 23, 2014 at 17:59
Bergi's user avatar
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 thejsp code? You shouldask a new question, post the HTTP request and your serverside code there.
0

It is not correct to useencodeURI() as this function encodes special character except:, / ? : @ & = + $ #

UseencodeURIComponent() to encode these characters.

For more info check below link:

http://www.w3schools.com/jsref/jsref_encodeuri.asp

answeredSep 23, 2014 at 18:03
Grish's user avatar

Comments

0

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?

answeredSep 23, 2014 at 18:05
Christopher Esbrandt's user avatar

Comments

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy 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.