Reputation: 300
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.
Upvotes: 2
Views: 4807
Reputation: 1276
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.
Upvotes: 2
Reputation: 300
this worked
var link = document.createElement("a");
var file = element.artifact;
link.download = element.artifactName;
link.href = URL.createObjectURL(file);
link.click();
Upvotes: 1