Skip to content

Instantly share code, notes, and snippets.

@shuson
Last active September 19, 2023 08:50
Show Gist options
  • Star 15 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save shuson/ca184a8abf597febc10f to your computer and use it in GitHub Desktop.
Save shuson/ca184a8abf597febc10f to your computer and use it in GitHub Desktop.
get base64 raw data of image from responseBody using jquery ajax
$.ajax({
type: "GET",
url: "imageURL",
beforeSend: function (xhr) {
xhr.overrideMimeType('text/plain; charset=x-user-defined');
},
success: function (result, textStatus, jqXHR) {
if(result.length < 1){
alert("The thumbnail doesn't exist");
$("#thumbnail").attr("src", "data:image/png;base64,");
return
}
var binary = "";
var responseText = jqXHR.responseText;
var responseTextLen = responseText.length;
for ( i = 0; i < responseTextLen; i++ ) {
binary += String.fromCharCode(responseText.charCodeAt(i) & 255)
}
$("#thumbnail").attr("src", "data:image/png;base64,"+btoa(binary));
},
error: function(xhr, textStatus, errorThrown){
alert("Error in getting document "+textStatus);
}
});
@hope965
Copy link

hope965 commented Jun 14, 2019

Working like a charm, thx :)

@Josloader3
Copy link

Thank you so much, I need this code from my captcha project :)

@scubbx
Copy link

scubbx commented Feb 24, 2020

Awesome snippet! :-)

@iam-msm
Copy link

iam-msm commented Nov 3, 2020

Thanks a lot. Worked Perfectly

@mrtanloveoflife
Copy link

Saved my day, thanks :D

@apunekar
Copy link

thank you so so so much..After hours of struggle....

@bigcake008
Copy link

My champion! 👯

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment