I’m loading a jpeg image from my server in binary format via XMLHttpRequest (I need it that way). It’s not base64 encoded.
Is it possible to turn it to an img object with javascript?
Thanks
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If the character encoding of the
XMLHttpRequesthas been set to something that won’t change the binary data, or you’ve set the response type, you can then run.responseTextthroughbtoa(putting it in base64 and letting you assign it as a data URI) or access.responsefor the binary data, respectively.Assuming your instance is named
xhrand you’re using the charset method beforexhr.sendbut afterxhr.opendothen when you’re
200 OKWhich you can then set as a src of an
<img>.Again assuming
xhr, this time.responsemethod; between.openand.send,Then at
200 OKWhich you can then set as a src of an
<img>. Example