Vuejs download a file from button click






















When the above link is clicked, the file is opened in a new browser tab, i need to prevent this default behavior and force a download upon click. The HTML 5 tag "download" is suppose to solve this problem doesn't seem to work. Chrome has recently deprecated the download tag form working with cross domain downloads. Does vue have a modifier to prevent this default? Are there any other ways to download the file either in javascript or in html?

One proposed solution is to read the URL as a arrayBuffer and then create a new blob in the DOM, and then create an anchor element and click it..

But that seems hacky to force a download of a file. I am sure their must be a cleaner solution to download a file form a URL, its a trivial problem, hoping for a simple solution. You can fetch the file as a blob and provide it the same way, there will be no request that leads into CORS issues.

Notes: I used Axios for my example but that's not a requirement, the blob's mime type is hardwired for the sake of simplicity. If you want the browser to handle the download instead of handling the download in JavaScript you need to use window. This gives a better user experience IMO but gets tricky to set up when trying to access authorization-protected content. To do this you need to store the authorization in cookies instead of relying on storing the authorization header in the browser's local storage.

I'm using Laravel and Vue. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.

Learn more. Asked 2 years, 11 months ago. Active 6 months ago. Viewed 75k times. Expected behavior : Download the file to the browser Actual behavior : Opens the file in a new tab Exception: Only images, pdf and browser compatible files are opened in a new tab, other files like.

Improve this question. Ricky-U Ricky-U 1 1 gold badge 4 4 silver badges 11 11 bronze badges. The url is a public url that contains a image, an example would be a googledrive public link image. AFAIK, the only current solution available is the "hacky" one you mentioned. Possible duplicate of Any fallback client-side solutions for the html5 download attribute?

HI Tony, thanks, yes i was also looking for a vue specific example, since vue has many methods to prevent default, i was hoping that it did.. Add a comment. Active Oldest Votes. Improve this answer. But before you tackle the big projects, we suggest you start by learning the basics. Now at it's lowest price ever! Approach 2: Using a custom javascript function firstly made a textarea where all the text input will be issued.

The click method simulates a mouse-click on an element. This requires a little intermediate knowledge of the JavaScript to work and in this example a Axios library will be used. Skip to content. Change Language. Related Articles. Table of Contents. Save Article. Improve Article. Like Article.

How to trigger a file download when. Welcome to GeeksforGeeks. Download Image. By clicking the download button.



0コメント

  • 1000 / 1000