Start file download after postback

In my application, I create a zip file for the user to download information about the exported database. A zip file is created when the user clicks the "generate data" button, and I register the request in the database.

On my page, I have a Gridview that shows the User’s download history, and also offers them the ability to download the last generated file for a certain period of time.

The problem I am facing is when they click the button, I want the page to refresh (thus updating the gridview and showing their last request), and then start downloading the files (IE, call a prompt and let them decide to open / save / cancel it).

I can start the download in the message, but my Gridview is not updated before it starts, so it does not display the newest request in the list. How can I get the gridview to update before the download request starts?

To start the download, use the following:

public void BeginDownload() { FileDownload download = InventoryService.GetLastThreeFileDownloads(this.EmployeeId).First(); FileInfo fi = new FileInfo(Server.MapPath(SERVER_DOWNLOAD_PATH) + download.DownloadFileName); Response.Clear(); Response.ContentType = "application/zip"; Response.AppendHeader("Content-Disposition", "attachment; filename=" + fi.Name); Response.TransmitFile(fi.FullName); Response.Flush(); } 

The method is called in the Page_Load event as the last element if the hidden field is set to true (which I set when they click the button to create the file).

I also tried doing this through jQuery / AJAX calls to either refresh the page or start the download with little success. I thought about opening a modal dialog and letting them click the link to start the download, and then refresh the page when the modal closes, but in a pinch if I cannot find another solution.

Any help is much appreciated!

+6
jquery c # gridview download
source share
3 answers

The fact is that you are not sending the page back to the user. The user clicks a button that sends an HTTP request for which you create an HTTP response containing a file to download. If you want to refresh the page and only after that send it the file to download, you need to send the usual result of the postback (without calling the BeginDownload method), and then somehow force it to execute another request, after which you will respond with the file.

There are various options here:

  • enable meta refresh on the page
  • use downloadable javascript
  • use iframe
  • ... or ask the user to click the link as you said.

All methods have their drawbacks (be careful, especially with regard to protecting IE unwanted downloads, which in some circumstances can be annoying), you should at least include a download link with "if the download does not start automatically, click here." )

+2
source share

Here's how I did something similar once:

  • Upon postback (generate data), send the updated page with the new data to gridview.
  • Add a little javascript function that executes when the page loads and that asks for the generated file (something like window.location='fileurl'; ). Of course, send this javascript function only after the postback.

The contents of the displayed page does not change when the file is requested, it simply opens the save dialog of your browser.

+3
source share

It looks like you are trying to do too much at once. I suggest sending back to refresh the gridview and then redirecting to a new highlighted page (or ashx handler) to load the file. The gridview page should remain visible when you use the content header in the ashx download page / handler.

+1
source share

All Articles