简体   繁体   中英

How to download in-memory file object in Javascript?

I have an in-memory file object like this in Javascript:

{
    name: "1_mRf78VMrVHjBMQpz6PYmiw.jpeg",
    lastModified: 1549023843303,
    lastModifiedDate: Fri Feb 01 2019 17:54:03 GMT+0530 (India Standard Time),
    webkitRelativePath: "",
    size: 265437,
}

How can I download it?

var link = document.createElement("a");
document.body.appendChild(link);
link.download =element.artifactName;//file name
link.href = element.artifact;//file object
link.click();

It is downloading corrupted file instead of original file.

this worked

var link = document.createElement("a");
var file = element.artifact;

link.download = element.artifactName;
link.href = URL.createObjectURL(file);
link.click();

Sorry, I had difficulty following your solution. I was able to come up with this:

var a = document.createElement('a');
a.href = window.URL.createObjectURL(file);
a.download = 'right.png';
a.click();

Where the file on line 2 is a reference to the file object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM