Catscarlet
Catscarlet

Reputation: 587

How to get FileType from a DOM with JavaScript?

Here is a DOM like this.

<img id="this-is-the-image" src="http://192.168.1.100/Image_tmp/2016-06/d4eb8d">

I need to click a button, run a JS, and download this image file.

I have finished the button and the download script.

Some code:

```

function downloadFile(fileName, url) {
        var aLink = document.createElement('a');
        var blob = new Blob([url]);
        var evt = document.createEvent("HTMLEvents");
        getImageType(blob);
        evt.initEvent("click", false, false);
        aLink.download = fileName;
        aLink.href = URL.createObjectURL(blob);
        aLink.dispatchEvent(evt);
    }

```

I have a problem. I can only get the src "http://192.168.1.100/Image_tmp/2016-06/d4eb8d" or the name d4eb8d, but actually the image is .png or .jpg. The brower can view it, but after I save it to my computer, the file name turn to be d4eb8d, not d4eb8d.png or d4eb8d.jgp. How can I get the real type of the image so that I can specify the download_name?

Upvotes: 0

Views: 199

Answers (3)

guest271314
guest271314

Reputation: 1

You can use XMLHttpRequest() with .responseType set to "blob" to request image file; blob.type.split("/")[1] where type is MIME type of Blob, [1] after .split() would be jpg, jpeg, png or other image type

  window.onload = function() {
    var aLink = document.querySelector("a");
    var fileName = "image";
    var request = new XMLHttpRequest();
    request.responseType = "blob";
    request.open("GET", "http://example.com/d4eb8d");
    request.onload = function() {
    var blob = this.response;
      var type = blob.type.split("/")[1];
      console.log(type);
      var evt = document.createEvent("HTMLEvents");
      evt.initEvent("click", false, false);
      aLink.download = fileName + "." + type;
      aLink.href = URL.createObjectURL(blob); 
      aLink.dispatchEvent(evt);
    }
    request.send()
  }

plnkr http://plnkr.co/edit/To4uZXL8PUph9qG3azvZ?p=preview

Upvotes: 1

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146563

You have an instance of HTMLImageElement and, as far as I can tell from the documentation, it doesn't have any property with the image type.

You could grab the URL, launch an AJAX call and get the Content-Type header, from which it should be easy to figure out a file extension... assuming the remote server provides the information (if it doesn't, you have a different problem). The XMLHttpRequest object has a .getResponseHeader() method.

You algo have the poor-man's solution of changing window.location to the picture URL but of course there's no way to force a download.

Last but not least, you can build a little server-side script that downloads the file on the server and prepares all the details.

Upvotes: 0

Alex Nikulin
Alex Nikulin

Reputation: 8689

Are you sure that new Blob([url]) will return blob with image content? not a text file with this content http://192.168.1.100/Image_tmp/2016-06/d4eb8d?

As i know you can turn image link to blob only by read this with XHR => How to get a file or blob from an object URL?

then when you will have blob, get file type from blob.type. And attach extension

var ext ="";
switch(blob.type){
   case "image/jpeg":
        ext=".jpg";
        break;
   case "image/png":
        ext=".png";
        break;
   case "image/gif":
        ext=".gif";
        break;
}
aLink.download = fileName + ext;

Upvotes: 0

Related Questions