How to force browser to download file using vue.js?
10,752
That's a normal behavior, considering that you're encoding the URL itself instead of its content.
You can get desired result another way:
<a :href="currentMediaUrl" download><i class="fa fa-download"></i></a>
Author by
Milkyway
Updated on December 08, 2022Comments
-
Milkyway over 1 year
I'd like to let users to download images by clicking on a download icon:
The image file is already fetched from the server and displayed:
<img :src="currentMediaUrl">
The button is intended to force the browser to download the image above:
<i @click="downloadMedia(currentMediaUrl)" class="fa fa-download"> </i>
Here is what I've tried:
downloadMedia(media) { let uriContent = "data:application/octet-stream," + encodeURIComponent(media); window.open(uriContent, 'neuesDokument'); },
But it downloads a file containing the media URI instead of the very file.
How can I fix it?
-
Rishabh Batra almost 4 yearsThanks a lot. I wanted to download the file on click and this helped.
-
-
Milkyway over 5 yearsDoes not work. Also the class need a
"
at the end. -
Milkyway over 5 yearsThis works but not as intended. My image is displayed inside a
modal
gallery. I want the file just being downloaded, not being displayed instead of the modal. -
Milkyway over 5 yearsAlright. Please refer to my comment on other answer on why it does not work for me.
-
Sumurai8 over 5 yearsI am unsure what you mean. The download attribute is pretty universally supported, except for safari on iphone and internet explorer. On those browsers this will result in a (new) page being opened to display the image. You could combine it with
target="_blank"
to force to open a new page ifdownload
is not supported. That sounds like a reasonable trade-off to me. If you don't want to download something else, just replacecurrentMediaUrl
. If you mean something else, I am confused by what that is. -
Milkyway over 5 yearsI'm developing on Chrome browser. After adding
target="_blank"
to thea
link, when it is clicked, the image is opened in a new tab and the active window becomes the new tab. What I want is the browser to just download the file, without displaying it. -
Sumurai8 over 5 yearsIf you run the code snippet above and click "Download", does it download a 100x100 semi-transparent square, or does it show you an image somehow?
-
Milkyway over 5 yearsWhen I click the download link in the snippet above, it displays a dialog box, to choose whether I want to open or save
myimage.png
. That's not what I want. I want the browser just downloads the image quietly. -
Sumurai8 over 5 yearsThat download dialog is a browser setting. Afaik you cannot bypass that, nor do you want to bypass that. Since the example works, I am guessing that you did not correctly implement the download link. Inspect the generated html and verify that the
download
attribute is on the html. Also verify that you do not have click handlers interfering with the native behaviour of the download link. -
Kunal Rajput about 2 yearsIn my case this works perfectly thanks