Html.action to download a file






















I think I would use a http handler. Example of what the handler might look like based on this SO question. Note though - this is off the top of my head so code will need tweaking to work.

Just thought I'd pop in how i would start off. I provided the example in asp. CrabBucket - Here's how I solved my issue!! Works great for me : I was using a controller action to initiate this download from this server side this is why I did not use a handler. Returning an http response from the controller seemed tricky initially but then I figured out a way to cheat the thing ; I had to write the following code inside my controller action -.

Use the basic Try-Catch, etc as per your wisdom : A lot of thanks to various web-forums and also to CrabBucket the SO question from where you posted the code, had an answer which was very much help! Sorry for multiple edits! My code failed for a moment. I guess, loops are always the way to go!!!

This worked for me and personally, I prefer to stick with whatever default customizations are provided. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Stefanos Chrs Stefanos Chrs 1, 2 2 gold badges 14 14 silver badges 44 44 bronze badges. NicholasKyriakides Kind of reminds me of this gem: image.

BryanLarsen You are right, Firefox doesn't allow this without adding the element to body first. Thank you, updating the answer — Stefanos Chrs. Is there a way that javascript function be triggered once the download finishes? Just trying to show a message once downloads start and remove the message once download completes. This is best solution out here and should be accepted answer. Show 6 more comments. Mark Amery k 66 66 gold badges silver badges bronze badges.

Matt Ball Matt Ball k 95 95 gold badges silver badges bronze badges. Perfect, thanks. Do you happen to know if most servers will set the Content-Disposition to 'attachment' by default? There is no "most. Don't rely on it being set. This issue has been driving me ballistic, and this was the only option that worked and is supported by IE. No jquery.

This doest work if you're trying to download an image, it would open the image in the browser — Dheeraj. Danubian Sailor Danubian Sailor It does, at least if you actually apprnf the iframe to document.

This doesn't seem to be working in Chrome right now, although it used to work. I wonder if it kind of intermittently stops working in different versions of Chrome. Works in Chrome as of Version Does not work with images in Firefox v It just renders the image in the iframe. This will fail silently if the file is of a type the browser knows how to render itself, though unless the server returns a Content-Disposition header indicating the file should be downloaded.

The question is explicitly asking how to do this with a button instead of a link. Delconis Delconis 2 2 silver badges 9 9 bronze badges. This at least the first implementation doesn't necessarily trigger a download.

You can hide the download link and make the button click it. Starwarswii Starwarswii 1, 1 1 gold badge 14 14 silver badges 14 14 bronze badges. This prevents warning messages from popping up. To handle the looping part, we use setTimeout , which is necessary for it to work in IE. Update : I am aware that the "run code snippet" no longer works, but that's due to cross site cookie issues.

The code works fine if deployed on your own site. I suppose you could initiate multiple file downloads using a bunch of iframes or popups, but from a usability standpoint, a ZIP file is still better. Who wants to click through ten "Save As" dialogs that the browser will bring up?

I agree that a zip file is a neater solution But if you have to push multiple file, here's the solution I came up with. Chrome will display a message to user to obtain his agreement to download multiple files the first time your site use it.

The only side effect of this technique, is that user will see a delay between submit and the download dialog showing. To minimize this effect, I suggest you use the technique describe here and on this question Detect when browser receives file download that consist of setting a cookie with your file to know it has started download.

You will have to check for this cookie on client side and to send it on server side. Don't forget to set the proper path for your cookie or you might not see it.

You will also have to adapt the solution for multiple file download. The browsers seem to have issues when you try to do it too fast So I used a timeout. To solve this, I created a JS library to stream multiple files directly into a zip on the client-side. The main unique feature is that it has no size limits from memory everything is streamed nor zip format it uses zip64 if the contents are more than 4GB.

Find "downzip" it on npm or github! Thus community wiki. I am looking for a solution to do this, but unzipping the files in javascript was not as clean as I liked. I decided to encapsulate the files into a single SVG file.

Edit: The SVG worked very well. If you are only going to download the files, ZIP might be better. If you are going to display the files, then SVG seems superior. When using Ajax components it is possible to start multiple downloads. Create an AjaxButton and override onSubmit. Create an AbstractAjaxTimerBehavior and start downloading. Getting list of url with ajax call and then use jquery plugin to download multiple files parallel. Active Oldest Votes.

Improve this answer. Nikhil Mittal Nikhil Mittal 2 2 silver badges 15 15 bronze badges. Sign up or log in Sign up using Google. Sign up using Facebook. Sign up using Email and Password. Post as a guest Name. 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.



0コメント

  • 1000 / 1000