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.

To supply this even more information, supposing you have a MVC Viewpoint that has a kind bound to a Concept course, permits get in touch with the Style ReportVM.

This takes out any sort of requirement for any sort of bodily files to built and also saved money on the server, so no housekeeping programs needed as well as when again this is seamless to the point individual.

A common scenario in my MVC applications is actually mentioning via an internet webpage that has some user configured report requirements (Day Ranges, Filters etc). When the individual has described the guidelines they post all of them to the server, the report is actually generated (state for example an Excel report as outcome) at that point I always keep the resulting documents as a byte variety in the TempData jug along with an unique referral. This reference is passed back as a Json Result In my AJAX feature that consequently reroutes to unique controller activity to take out the info coming from TempData and also download and install throughout users browser.

If I’m performing a full message back but I can not receive it dealing with AJAX in mvc, my stand out report profits fine.

Another customization that might rapidly be actually served if needed is actually to pass the MIME Kind of the file as a 3rd parameter in order that the one Controller action could the right way offer a variety of result data styles.

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 not directly give back a declare download via an AJAX phone call thus, an alternate procedure is actually to utilize an AJAX contact us to submit the associated records to your hosting server. You may then use hosting server side code to generate the Excel Data (I will suggest making use of EPPlus or even NPOI for this although it sounds as if you have this component operating).

From the end users perspective, the data download function is smooth as they certainly never ever before leave behind the web page on which the demand controls.

As soon as the file has actually been actually created on the server pass back the pathway to the file (or even just the filename) as the come back worth to your AJAX call and also after that prepared the JavaScript window.location to this LINK which will induce the world wide web browser to install the file.

you do not call for to always keep the excel as a bodily report on the server – instead, store it in the (Session) Cache. Use an exclusively produced name for your Store variable (that establishments that excel file) – this will certainly be actually the return of your (initial) ajax telephone call. By doing this you do not need to cope with report gain access to concerns, regulating (erasing) the files when certainly not required, etc. and, possessing the file in the Cache, is quicker to secure it.

Leave a Reply

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