Skip to content

Instantly share code, notes, and snippets.

@shuson

shuson/sample.js

Last active Nov 19, 2020
Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@hope965 hope965 commented Jun 14, 2019

Working like a charm, thx :)

@Josloader3

This comment has been minimized.

Copy link

@Josloader3 Josloader3 commented Jul 11, 2019

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

@scubbx

This comment has been minimized.

Copy link

@scubbx scubbx commented Feb 24, 2020

Awesome snippet! :-)

@masum2444

This comment has been minimized.

Copy link

@masum2444 masum2444 commented Nov 3, 2020

Thanks a lot. Worked Perfectly

@mrtanloveoflife

This comment has been minimized.

Copy link

@mrtanloveoflife mrtanloveoflife commented Nov 19, 2020

Saved my day, thanks :D

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