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
  • 5,345
  • 8
  • 38
  • 40
user3201607
  • 79
  • 1
  • 1
  • 9

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 use encodeURIComponent:

document.location.href = 'display.jsp?img='+encodeURIComponent(uri);
Bergi
  • 572,313
  • 128
  • 898
  • 1,281
  • display.jsp page landing as empty page. why – user3201607 Sep 23 '14 at 18:06
  • Maybe there's a mistake in the `jsp` code? You should [ask a new question](http://stackoverflow.com/questions/ask), post the HTTP request and your serverside code there. – Bergi Sep 23 '14 at 18:12
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

Grish
  • 822
  • 2
  • 10
  • 22
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?

Community
  • 1
  • 1
Christopher Esbrandt
  • 1,188
  • 10
  • 21