3

I followed this answer & file downloading is successful. The facing problem is to set downloaded image file into the img.src tag.

Image link: https://images.pexels.com/photos/853199/pexels-photo-853199.jpeg?crop=entropy&cs=srgb&dl=aerial-view-of-seashore-near-large-grey-rocks-853199.jpg&fit=crop&fm=jpg&h=4000&w=6000

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.

enter image description here

Mount Ain
  • 316
  • 3
  • 15
  • Does it at least show the icon for not-found images (something like [this](https://cdn.dribbble.com/users/93493/screenshots/1445193/notfound.png))? – Pine Code Mar 03 '20 at 14:26
  • @RHShanks92, Throws error says `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.` – Mount Ain Mar 03 '20 at 14:36
  • And [this one](https://lh3.googleusercontent.com/-gCA72TalLNU/VyZdUQ2AFCI/AAAAAAAAAFA/4fTrT4GaObMs2w1RO-11tMug3h8fec5AACLcB/s1600/Unloaded%2BPicture.JPG)? – Pine Code Mar 03 '20 at 14:39
  • @RHShanks92, Yes, 2nd one loads data. But same blank area. I updated question with your links – Mount Ain Mar 03 '20 at 14:47

2 Answers2

3

btoa receive a string as an argument, but you have a stream. You can use URL.createObjectURL for get a blob url

const url = 'https://images.pexels.com/photos/853199/pexels-photo-853199.jpeg?crop=entropy&cs=srgb&dl=aerial-view-of-seashore-near-large-grey-rocks-853199.jpg&fit=crop&fm=jpg&h=4000&w=6000';
const img = document.querySelector('img');

fetch(url).then(data => data.blob()).then(blob => {
    const src = URL.createObjectURL(blob);
    img.src = src;
}).catch(err => console.log(err));
<img height="150"/>

For download an image from url:

var a = document.createElement('a');
a.href='https://images.pexels.com/photos/853199/pexels-photo-853199.jpeg?crop=entropy&cs=srgb&dl=aerial-view-of-seashore-near-large-grey-rocks-853199.jpg&fit=crop&fm=jpg&h=4000&w=6000';
a.download='filname.jpg';
document.body.appendChild(a);
a.click();
a.remove()
Yosef Tukachinsky
  • 5,570
  • 1
  • 13
  • 29
  • I am using the concept for download image file using pure JS and set it to the img tag. I followed this answer: https://stackoverflow.com/a/44766402/12708425 – Mount Ain Mar 03 '20 at 14:54
  • for download you not need all this staff.. `var a = document.createElement('a');a.href='url';a.download='filname.jpg';document.body.appendChild(a);a.click();a.remove()` – Yosef Tukachinsky Mar 03 '20 at 15:19
  • sorry for misleading, I want to show progress for corresponding image download. That's why I used that code from the answer that I have attached in question. – Mount Ain Mar 05 '20 at 03:40
0

One long way is to use FileReader that assuming you received the data as blob so this should work (hopefully) :

xml = new XMLHttpRequest()
xml.open("GET", "YOUR URL", true)
xml.responseType = "blob"
var blob
xml.onload = function(e){blob = xml.response;}
file = new FileReader()
var base64
file.onloadend = function() {base64 = file.result}
file.readAsDataURL(blob)
img = document.getElementById('downloaded-img')
img.src = base64
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320