Asp Net Mvc 5 Download File
In this article you will learn about how to upload and download files in ASP.NET MVC. How to return/Download a file as response? How a browser knows what file type is returned from the server? The Content-Type header is the one that says the browser what kind of file is being returned from the server. For example, to return a pdf file from. I wanted to download that zip file directly on user machine after it is created. Downloading of zip file through ASP.NET MVC using DotNetZip. C# asp.net-mvc-5.
- ASP.NET MVC 5 - Import/Export CSV File. The end-user then downloads the CSV file which behind the scenes on the web server is already exported to a.CSV file.
- This class is used to send binary file content to the response. In the following sample, we will see how we can leverage the FileResult action to download files in an ASP.NET MVC Web Application. Step 1: Open VS2010 and create a new ASP.NET MVC 3 project, name it as ‘MVC3ReturningFiles’.
I have a large(ish) form in MVC.
I need to be able to generate an excel file containing data from a subset of that form.
The tricky bit is that this shouldn't affect the rest of the form and so I want to do it via AJAX. I've come across a few questions on SO that seem to be related, but I can't quite work out what the answers mean.
This one seems the closest to what I'm after: asp-net-mvc-downloading-excel - but I'm not sure I understand the response, and it is a couple years old now. I also came across another article (can't find it anymore) about using an iframe to handle the file download, but I'm not sure how to get this working with MVC.
My excel file returns fine if I'm doing a full post back but I can't get it working with AJAX in mvc.
13 Answers
You can't directly return a file for download via an AJAX call so, an alternative approach is to to use an AJAX call to post the related data to your server. You can then use server side code to create the Excel File (I would recommend using EPPlus or NPOI for this although it sounds as if you have this part working).
UPDATE September 2016
My original answer (below) was over 3 years old, so I thought I would update as I no longer create files on the server when downloading files via AJAX however, I have left the original answer as it may be of some use still depending on your specific requirements.
A common scenario in my MVC applications is reporting via a web page that has some user configured report parameters (Date Ranges, Filters etc.). When the user has specified the parameters they post them to the server, the report is generated (say for example an Excel file as output) and then I store the resulting file as a byte array in the TempData
bucket with a unique reference. This reference is passed back as a Json Result to my AJAX function that subsequently redirects to separate controller action to extract the data from TempData
and download to the end users browser.
To give this more detail, assuming you have a MVC View that has a form bound to a Model class, lets call the Model ReportVM
.
First, a controller action is required to receive the posted model, an example would be:
The AJAX call that posts my MVC form to the above controller and receives the response looks like this:
The controller action to handle the downloading of the file:
One other change that could easily be accommodated if required is to pass the MIME Type of the file as a third parameter so that the one Controller action could correctly serve a variety of output file formats.
This removes any need for any physical files to created and stored on the server, so no housekeeping routines required and once again this is seamless to the end user.
Note, the advantage of using TempData
rather than Session
is that once TempData
is read the data is cleared so it will be more efficient in terms of memory usage if you have a high volume of file requests. See TempData Best Practice.
ORIGINAL Answer
You can't directly return a file for download via an AJAX call so, an alternative approach is to to use an AJAX call to post the related data to your server. You can then use server side code to create the Excel File (I would recommend using EPPlus or NPOI for this although it sounds as if you have this part working).
Once the file has been created on the server pass back the path to the file (or just the filename) as the return value to your AJAX call and then set the JavaScript window.location
to this URL which will prompt the browser to download the file.
From the end users perspective, the file download operation is seamless as they never leave the page on which the request originates.
Below is a simple contrived example of an ajax call to achieve this:
- url parameter is the Controller/Action method where your code will create the Excel file.
- data parameter contains the json data that would be extracted from the form.
- returnValue would be the file name of your newly created Excel file.
- The window.location command redirects to the Controller/Action method that actually returns your file for download.
A sample controller method for the Download action would be:
CSLCSLMy 2 cents - you don't need to store the excel as a physical file on the server - instead, store it in the (Session) Cache. Use a uniquely generated name for your Cache variable (that stores that excel file) - this will be the return of your (initial) ajax call. This way you don't have to deal with file access issues, managing (deleting) the files when not needed, etc. and, having the file in the Cache, is faster to retrieve it.
AGuyCalledGeraldI was recently able to accomplish this in MVC (although there was no need to use AJAX) without creating a physical file and thought I'd share my code:
Super simple JavaScript function (datatables.net button click triggers this):
C# Controller code:
In the ExportHelper class I do use a 3rd party tool (GemBox.Spreadsheet) to generate the Excel file and it has a Save to Stream option. That being said, there are a number of ways to create Excel files that can easily be written to a memory stream.
In IE, Chrome, and Firefox, the browser prompts to download the file and no actual navigation occurs.
I used the solution posted by CSL but I would recommend you dont store the file data in Session during the whole session. By using TempData the file data is automatically removed after the next request (which is the GET request for the file). You could also manage removal of the file data in Session in download action.
Session could consume much memory/space depending on SessionState storage and how many files are exported during the session and if you have many users.
I've updated the serer side code from CSL to use TempData instead.
First Create the controller action that will create the Excel File
then create the Download action
if you want to delete the file after downloaded create this
and finally ajax call from you MVC Razor view
This thread helped me create my own solution that I will share here. I was using a GET ajax request at first without issues but it got to a point where the request URL length was exceeded so I had to swith to a POST.
The javascript uses JQuery file download plugin and consists of 2 succeeding calls. One POST (To send params) and one GET to retreive the file.
Server side
MachinegonMachinegonCSL's answer was implemented in a project I'm working on but the problem I incurred was scaling out on Azure broke our file downloads. Instead, I was able to do this with one AJAX call:
SERVER
CLIENT(modified version of Handle file download from ajax post)
wilsjdwilsjdThe accepted answer didn't quite work for me as I got a 502 Bad Gateway result from the ajax call even though everything seemed to be returning fine from the controller.
Perhaps I was hitting a limit with TempData - not sure, but I found that if I used IMemoryCache instead of TempData, it worked fine, so here is my adapted version of the code in the accepted answer:
AJAX call remains as with the accepted answer (I made no changes):
The controller action to handle the downloading of the file:
..
Now there is some extra code for setting up MemoryCache..
In order to use '_cache' I injected in the constructor for the controller like so:
And make sure you have the following in ConfigureServices in Startup.cs:
Asp Net Mvc 5 Download File For Pc
I may sound quite naive, and may attract quite a criticism, but here's how I did it,
(It doesn't involveajax for export, but it doesn't do a full postbackeither )
Thanks for this post and this answer.
Create a simple controller
Nothing there. I downloaded the net framework too but it turns out Vista (not on this 'internet' machine) already has net framework installed - i dont know if that's just mine or all Vista machines as my dad bought this one for us as he knows my old machine was getting too small to run the sims 2. Sims 2 file maid download. Hi, just to let you know that i downloaded this yesterday after finding my trusty Sim File Maid 2 didnt work on my new Vista machine. My computer promptly had a temper tantrum, told me the page didn't exist and crashed.typical.I'll just stick to the original file maid until your husband codes one for an older version of the.net frameworkthanks anyway, looked like a nice program.
And here are the views. or the View
The whole point of trick seems here that, we are posting a form in mid of a Razor View, calling an Action method which returns a FileResult, and within the return we return a File.
And for posting the filter values, as said, ( and if you require to), I am posting those in another action, as has been attempted to describe.
I am using Asp.Net WebForm and just I wanna to download a file from server side. There is a lot article but I cannot find just basic answer.Now, I tried a basic way and got it.
Asp Net Mvc 5 Tutorial
That's my problem.
I have to create a lot of input button dynamically on runtime. And I want to add each button to download button with giving an unique fileNumber.
I create each button like this:
Each button call this ajax method.
Then I wrote a basic simple method.
I am generating this Form_1, Form_2, Form_3.. And I am going to delete this old files with another program. But if there is a way to just sending byte array to download file like using Response. I wanna to use it.
I hope this will be usefull for anyone.