1

I am trying to force a download of an XML file when the user visits a page.

This is the code I am using

public partial class GenerateTemplate : LayoutsPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //.............
        //Going about generating my XML
        //.............
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=template.xml");
        Response.Write(xmlDoc.InnerXml);
        Response.Flush();
        Response.Close();
    }
}

I am facing a problem that my download window hangs indefinitely, without ever completing the download/Open of the file.

What am I doing wrong? Am I not disposing any objects or closing any connections here?

Stephen Kennedy
  • 20,585
  • 22
  • 95
  • 108
ashwnacharya
  • 14,601
  • 23
  • 89
  • 112

4 Answers4

7

I've posted a reply to a similar question. To quote myself:

Just a small addition to the other answers. At the very end of a download I execute:

context.Response.Flush();
context.ApplicationInstance.CompleteRequest();

I learned that otherwise, the download sometimes does not complete successfully.

This Google Groups posting also notes that Response.End throws a ThreadAbortException which you could avoid by using the CompleteRequest method.

Community
  • 1
  • 1
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • Is it a good habit to reply in such a manner or should I post this as a comment only to the initial question? – Uwe Keim Jan 03 '11 at 09:14
  • 1
    CompleteRequest and Response.End are completely different. CompleteRequest does -not- stop the rest of the ASP.NET pipeline from processing; it only affects the HTTP pipeline. You're in the ASP.NET part of the HTTP pipeline, and the asp.net handler will continue processing unless you call Response.End. – David Eison Aug 16 '12 at 00:21
2

Try using Response.End() instead of Flush and Close

That's what I have been using in the past.

Madhur Ahuja
  • 22,211
  • 14
  • 71
  • 124
0

Have you tried setting the content type and calling Response.End():

protected void Page_Load(object sender, EventArgs e)
{
    //.............
    //Going about generating my XML
    //.............
    Response.Clear();
    Response.ContentType = "text/xml";        
    Response.AddHeader("Content-Disposition", "attachment; filename=template.xml");
    Response.Write(xmlDoc.InnerXml);
    Response.End();
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
-2

You say you want to have a file download With asp.net web forms you would do this:

context.Response.ContentType = //MIME type

// Set the filename 
context.Response.AddHeader("content-disposition", "attachment;filename=" + queryFile); 

// Stream the file to the client 
context.Response.WriteFile(file);
gideon
  • 19,329
  • 11
  • 72
  • 113
  • I do not have a file in a physical server. The XML file exists as an XMLDocument object in the memory. For this reason, I cannot use WriteFile() method. – ashwnacharya Jan 03 '11 at 09:31
  • ok! Sorry, I thought the `WriteFile` method would have an overload that takes a stream (Much like mvc's controller.File does) Maybe you can try Writing to the output stream. See :http://msdn.microsoft.com/en-us/library/system.web.httpresponse.outputstream.aspx – gideon Jan 03 '11 at 09:54