B Donald
B Donald

Reputation: 103

Response.WriteFile stopping previous request

I have a button that calls a click event to download a picture. In my click event I have some code to change my display as the Save dialog pops up. The issue I am having is that when the dialog comes up it stops the previous line of code from executing. How can I check to see that my code has executed before calling the function?

Here is my button event handler and function:

 protected void btnSave_Click(object sender, EventArgs e)
{
    var _title          = txtTitle.Text;
    txtTitle.Text       = string.Empty;
    txtDescription.Text = string.Empty;

    ChangeDisplay();

    if (Request.Browser.Browser == "IE")
        lblSnapShot.Visible = (lblSnapShot.Visible) ? false : true;

    SaveSnapShot(_title);
}

void SaveSnapShot(string _title)
{
    try
    {
        FileInfo _fileToDownload = new FileInfo(_path);
        Response.Clear();
        Response.ContentType = "image/png";
        Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + _title + ".png\"");
        Response.WriteFile(_fileToDownload.FullName);

        HttpContext.Current.ApplicationInstance.CompleteRequest();
    }
    catch (System.Exception ex)
    {
        ChangeDisplay();

        if (Request.Browser.Browser == "IE")
            lblSnapShot.Visible = (lblSnapShot.Visible) ? false : true;

        Response.Write("SAVE FAILED \n Error: " + ex.Message);
    }
}

Upvotes: 0

Views: 684

Answers (1)

Robert Levy
Robert Levy

Reputation: 29073

Response.WriteFile doesn't stop the processing of the other code (that code comes before you even call WriteFile so how could it?)

However, your SaveSnapshot method is sending back to the browser an image instead of the usual response of HTML. For each request from the browser, you can only send one response - you can't send back an image file and update HTML at the same time.

Upvotes: 2

Related Questions