ASP.NET Excel file download on button click

To preface, all this happens on a regional intranet, which does not at any point requirement to connect to the internet.

I have a database on which I run a question, after which the user presses a “Download Spreadsheet” button which creates/sends the spreadsheet. The creation of the spreadsheet works fine, however after many efforts I can not get the file to download. Here’s what I have actually tried:

Customizing the Response/Header things
TransmitFile
WriteFile
BinaryStream
Redirect
Javascript Redirect
Response.Write( javascript code).

For the most parts, the result is that the excel file is created, but no redirect/download happens. When it comes to Response.Redirect(), if it’s a site it works excellent, however if it’s a redirect to a file:///, then it tosses a thread exception but say goodbye to information.

I believe it relates to the lifecycle of an ASP.NET file, however I hesitate I am not experienced enough with ASP.NET to be able to know for sure.

I am attempting to bring a stand out file stored in bytes from database from an aspx web page and download and open in.xlsx format, however the file is getting downloaded as.aspx. Ways to solve this?

I require to have the ability to create an excel file consisting of information from a subset of that kind.

The challenging bit is that this should not affect the remainder of the type and so I want to do it by means of AJAX. I’ve encountered a few questions on SO that appear to be associated, however I cannot rather work out what the responses mean.

This one seems the closest to exactly what I want: asp-net-mvc-downloading-excel – however I’m not exactly sure I understand the action, and it is a couple years of ages now. I also stumbled upon another short article (can’t find it any longer) about using an iframe to manage the file download, but I’m not sure ways to get this working with MVC.

If I’m doing a full post back but I can’t get it working with AJAX in mvc, my excel file returns fine.

A typical scenario in my MVC applications is reporting by means of a web page that has some user configured report specifications (Date Ranges, Filters and so on). When the user has defined the parameters they publish them to the server, the report is produced (say for instance an Excel file as output) then I keep the resulting file as a byte array in the TempData pail with a special recommendation. This referral is passed back as a Json Lead To my AJAX function that subsequently reroutes to different controller action to draw out the information from TempData and download to the end users browser.

To provide this more information, assuming you have a MVC View that has a form bound to a Design class, lets call the Model ReportVM.

Another modification that could quickly be accommodated if needed is to pass the MIME Type of the file as a 3rd 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 developed and saved on the server, so no housekeeping regimens needed and when again this is seamless to the end user.

Note, the advantage of utilizing TempData instead of Session is that as soon as TempData is checked out the information is cleared so it will be more efficient in regards to memory usage if you have a high volume of file demands. See TempData Best Practice.

You can’t straight return a file for download via an AJAX call so, an alternative technique is to utilize 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 actually been developed on the server pass back the path to the file (or just the filename) as the return value to your AJAX call and after that set the JavaScript window.location to this URL which will trigger the internet browser to download the file.

From the end users viewpoint, the file download operation is seamless as they never ever leave the page on which the request stems.

you don’t require to keep the excel as a physical file on the server – instead, shop it in the (Session) Cache. Utilize an uniquely created name for your Cache variable (that stores that excel file) – this will be the return of your (initial) ajax call. By doing this you don’t have to deal with file access concerns, managing (erasing) the files when not needed, etc. and, having the file in the Cache, is quicker to obtain it.

Leave a Reply

Your email address will not be published. Required fields are marked *