Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable button after writing file to response

Tags:

c#

asp.net

There is many similar questions but there is still no clear answer that is solving the problem taking some action after writing some stream to response.

I have a following situation:

On button click I am generating some excel file that I am going to write to response allowing user to download generated file. Imidietly after clicking the button, I am disabling it, to prevent double clicking this button. In Page-Load event handler I have following code:

 GenerateBTN.Attributes.Add("onclick", "this.disabled=true;" + ClientScript.GetPostBackEventReference(GenerateBTN, "").ToString());

After Page_Load eventhandler, GenerateBTN_Click handler executes the code needed for generating the file and at the end of this method (handler) I am response writing generated file with following code:

Response.Clear();
Response.ContentType = "application/octet-stream";                
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
Response.WriteFile(@"C:\Reports\" + filename);
Response.End();    

The Save As dialog appears and user can download the generated file, but the problem is that the disabled GenerateBTN remains disabled. How to enable it afer Writing generated file to response? I understand that afer clearing current response I can not continue with the initial Response, but is there any way to solve this problem?

like image 878
Zzz Avatar asked Dec 14 '25 14:12

Zzz


1 Answers

You can put an IFrame on the page and set it's visiblity to hidden. Have your button load the file in the IFrame and use Javascript to detect if the IFrame is still loading or not. When the loading is done, enable your button.

Can't supply a code example at the moment, but if you decide to go this route and need a sample, let me know I will update this answer.

Edit for 2nd answer

What you want to do is create a file like "Download.aspx" that you pass in the file name as a querystring parameter. This way, you can have your server get the file from a location outside of the Web Application's path and adjust the header to force a file download.

Download.aspx

  using System.IO;
    protected void Page_Load(object sender, EventArgs e)
    {
        string FileName = Server.UrlDecode(Request.Params["FileName"]);  //Example: "MyFile.txt"

        Response.AppendHeader("Content-Type", "application/force-download");
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName);
        Response.WriteFile(@"C:\MyFolder\" + FileName);
    }

You would load the page by calling something like "Download.aspx?FileName=MyFile.txt"

You will need to add checks to make sure the file exists and the querystring parameter exists, but that should force the download and allow you to get the file from another location. When you use an ASPX page to serve the file, you can also do credential checks to see if the user is logged into your site (if you have login logic already) or log the download to a log file/database if you want to keep track of it. It gives you a lot more control over the download process.

As for the IFrame loading code, I'm not 100% sure how this works with a file download, but what I was originally thinking was something like this -- view source on: http://sykari.net/stuff/iframe.

like image 180
Jemes Avatar answered Dec 17 '25 07:12

Jemes