Last active
September 19, 2023 08:50
-
-
Save shuson/ca184a8abf597febc10f to your computer and use it in GitHub Desktop.
get base64 raw data of image from responseBody using jquery ajax
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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); | |
} | |
}); |
thank you so so so much..After hours of struggle....
My champion! 👯
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Saved my day, thanks :D