I followed this answer & file downloading is successful. The facing problem is to set downloaded image file into the img.src
tag.
Code:
function onReadyState(e){
let r = e.target;
if(r.readyState != 4 || r.status != 200){
return
}
console.log(r)
let img = document.getElementById('downloaded-img')
let base64 = btoa(r.response)
img.src = 'data:image/jpg;base64,'+base64
}
I tried to convert responseText into base64 to set img.scr
for display downloaded image. But I got error,
Uncaught DOMException: Failed to execute 'btoa' on 'Window': The string to be encoded contains characters outside of the Latin1 range. at XMLHttpRequest.downloadCompleted
Then I used below code by following this answer.
let base64 = btoa(unescape(encodeURIComponent(r.responseText)))
The error is gone. But img
is still whitespace. How can I resolve it? Thanks in advance...
Update: I used this link. It throws below error,
Access to XMLHttpRequest at 'https://cdn.dribbble.com/users/93493/screenshots/1445193/notfound.png' from origin 'null' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.
I used this link too, Didn't got any error But I got same blank area instead of image.