We want to download a file when we click on an anchor tag link Download this file. Implementation and Folder structure is as shown below. Skip to content. Change Language. Related Articles. Table of Contents. Save Article. Improve Article. This does not work if your filePath has a querystring since submitting a form will overwrite the querystring in the action attribute.
This also closes the websockets. This looks like a really complicated way to set window. Just window. Regardless of whether there's a downside to this solution, per se, you haven't provided any upside to using this over a link. And there is a downside: you can't use the download attribute this way to tell the browser that you want a download regardless of what headers the server returns, which you can do with an a element.
I ended up using the below snippet and it works in most browsers, not tested in IE though. Abk Abk 1, 1 1 gold badge 19 19 silver badges 28 28 bronze badges. What's the point of using MouseEvent here instead of always using click?
And why append the link to the document before clicking it? Maybe this has advantages over the simpler approach shown in stackoverflow. It's been a while I posted this answer. I can't remember if there's any reason behind those unnecessary lines of code. Maciej Krawczyk Maciej Krawczyk 11k 5 5 gold badges 32 32 silver badges 45 45 bronze badges. La masse La masse 1, 1 1 gold badge 8 8 silver badges 24 24 bronze badges. How is this an improvement?
It appears to simply be a more complicated way of doing the same thing. Urmas Repinski Urmas Repinski 1 1 silver badge 6 6 bronze badges. File is getting downloaded but its always corrupt. Any suggestions? Thank you — Shrivaths Kulkarni. Maybe just have your javascript open a page that just downloads a file, like when you drag a download link to a new tab: Window. This creates a popup window, which most browsers block — Ashton Wiersdorf.
This is just the same approach as shown in Imagine Breaker's answer years earlier, but with the added downside of requiring jQuery. Donny V. Itamar Smirra Itamar Smirra 46 5 5 bronze badges. It just requires a little adjusting: The PHP file can call itself. I don't see the relevance here. Ifch0o1 Ifch0o1 1 1 gold badge 12 12 silver badges 31 31 bronze badges.
Fawad Ali Fawad Ali 5 5 silver badges 11 11 bronze badges. Note: Not supported in all browsers. Xero Kael Xero Kael 12 12 bronze badges. Community Bot 1 1 1 silver badge. Hitesh Hitesh 3, 10 10 gold badges 37 37 silver badges 80 80 bronze badges. Hope this could help someone out there.
This whole function body is just an overcomplicated way of doing window. The link you create isn't used for anything.
The Overflow Blog. Podcast what if you could invest in your favorite developer? Who owns this outage? Building intelligent escalation chains for modern SRE. Featured on Meta. Now live: A fully responsive profile. Reducing the weight of our footer. Linked 3. See more linked questions. Related Writing a PHP file handler to force the content disposition in the header will also take less time than an API, but still longer than Nick's approach.
If you know how to turn on an Apache mod and edit the. It requires Linux hosting not Windows. This may not be appropriate approach for all uses as it requires high level server access to configure. As such, if you have said access it's probably because you already know how to do those two things.
If not, check Nick's blog for more instructions. As the html5 way my previous answer is not available in all browsers, heres another slightly hack way.
Now set location. This will cause the browser to download the file. Unfortunately you can't set file name or extension this way. Fiddling with the media-type could yield something. I needed to do this for files created with dynamic names in a particular folder and served by IIS. If you are using HTML5 and i guess now a days everyone uses that , there is an attribute called download.
I've had some issues with the suggested solution that creates an a DOM element, and sets the download attribute. It still displayed a popup warning in some browsers perhaps they got a little stricter by Adding the pdf mime type to the href attribute solved the browser popup warning, but it messed up the file the downloaded file got damaged and couldn't be opened.
His code example uses an npm library though. Here's how to do it using js only:. The behaviour should depend on how the browser is set up to handle various MIME types. I recommend against this as it should be the users choice what will happen when they open a PDF file.
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 10 years, 4 months ago. Active 1 month ago. Viewed k times. Improve this question. You are free to use it for commercial as well as non-commercial use at your own risk, but you cannot use it for posting on blogs or other tutorial websites similar to www.
All the code samples and API provided by the authors are solely their creation and neither the author nor the site are responsible if it does not work as intended. I agree to the above terms. Download Required. What our readers say. Error Details. This site makes use of Cookies. Please refer Privacy Policy for more details.
0コメント