4

I have a problem with not getting a postback after Response.End();

So the scenario is that i will click a button, clean an xmlfile and generate a message (out msg), download the xml, and then show the message in a label on the webpage. The problem is that it doesn't do anything after Response.End().

protected void btnDoIt_Click(object sender, EventArgs e)
{
    string xml = "exampleXml";
    string fileName = "exampleName";

    MemoryStream ms = new MemoryStream();
    string msg;
    var cleanXml = CleanXml(xml, out msg);

    cleanXml.Save(ms);
    byte[] bytes = ms.ToArray();

    Response.Clear();
    Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.xml", fileName));
    Response.ContentType = "text/xml";
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();

    lblFeedback.Text = msg;
    lblFeedback.Visible = true;
}

I tried moving the code in front of response.End(), like below, but it didn't work as well for some reason.

protected void btnDoIt_Click(object sender, EventArgs e)
{
    string xml = "exampleXml";
    string fileName = "exampleName";

    MemoryStream ms = new MemoryStream();
    string msg;
    var cleanXml = CleanXml(xml, out msg);

    lblFeedback.Text = msg;
    lblFeedback.Visible = true;

    cleanXml.Save(ms);
    byte[] bytes = ms.ToArray();

    Response.Clear();
    Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.xml", fileName));
    Response.ContentType = "text/xml";
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();
}

I also tried to "Click" an invisible button to get the postback like below. Didn't work either.

protected void btnDoIt_Click(object sender, EventArgs e)
{
    string xml = "exampleXml";
    string fileName = "exampleName";

    MemoryStream ms = new MemoryStream();
    string msg;
    var cleanXml = CleanXml(xml, out msg);

    InvisibleButtonClick(msg)

    cleanXml.Save(ms);
    byte[] bytes = ms.ToArray();

    Response.Clear();
    Response.AddHeader("Content-Disposition", string.Format("attachment;filename={0}.xml", fileName));
    Response.ContentType = "text/xml";
    Response.BinaryWrite(bytes);
    Response.Flush();
    Response.End();
}

private void InvisibleButtonClick(string msg)
{
    lblXmlFeedback.Text = msg;
    lblXmlFeedback.Visible = true;
    InvisibleButton_OnClick(new object(), new EventArgs());
}

protected void InvisibleButton_OnClick(object sender, EventArgs e)
{

}

What am i missing here?

Brewsli
  • 127
  • 1
  • 10

1 Answers1

4

What you do not have understand is that the return from IIS to the browser can be one stream only.

In your case, you believe that you can send two.

What is actually happened?

A. You post back some data from the web page.

B. You return some file for download from the user.

C. There the stream end and close – nothing is going to the browser any more. So there is no way the page to refress, and there is no way the data on the page to change because you send nothing any more to the page.

How to solve this.

a) You can make a handler that download the file and you only give a link in the page, a link to the handler that download the data.

b) you can make some other page that you redirect him with the message that you like to send, and there automatically with some javascript you start downloading your file.

c) you can use ajax to download your extra data, and javascript to show any message

similar answers

What is the best way to download file from server
file download by calling .ashx page
Updating a page before initiating a download?

Aristos
  • 66,005
  • 16
  • 114
  • 150