0

I have a page with a list where can I download XML Files, but there is a server of a company that below the XML file is the page's HTML together. The XML on download code usually works on all servers where the application is installed except that server. The method for downloading the file is as follows:

 public void StreamFileToBrowser(string sFileName, byte[] fileBytes, string ext)
    {
        HttpContext context = HttpContext.Current;
        context.Response.Buffer = false;
        context.Response.Clear();
        context.Response.ClearHeaders();
        context.Response.ClearContent();
        context.Response.AppendHeader("content-length", fileBytes.Length.ToString());
        context.Response.ContentType = "application/octet-stream";//string.Format("application/{0}", ext);
        context.Response.AppendHeader("content-disposition", "attachment; filename=" + sFileName);
        context.Response.BinaryWrite(fileBytes);           
    }

Any alternative way to force file download?

EDIT

This method is called when the user click on download button.

Salatiel
  • 121
  • 2
  • 10
  • 5
    I'm having trouble making out the meaning of your first sentence, specifically the part after the comma: "but there is a server of a company that below the XML file is the page's HTML together". Could you either explain this or correct it? – Shotgun Ninja Sep 25 '15 at 20:43
  • Sorry with english. Isn't my native language. When I try to download the XML file, the HTML code come below together in the same file. – Salatiel Sep 25 '15 at 20:48
  • Are you the client side, or the server side? The way it looks in the code, you are the server side. – Ron Beyer Sep 25 '15 at 20:49
  • its heavily depended on the server API. My guess is that you should first make a HTTP request and then save the returned XML – Steve Sep 25 '15 at 20:50
  • @RonBeyer I'm the server side using C#. – Salatiel Sep 25 '15 at 20:52
  • @Steve can you give me a code example? – Salatiel Sep 25 '15 at 20:53

2 Answers2

2

After you write the file to the output. You should end the response by calling:

context.ApplicationInstance.CompleteRequest();

This prevents anything further from being written to the response. Usually this isn't necessary if you separate out your XML download logic into a separate handler. But when you have some logic that does both, you can run into issues.

mason
  • 31,774
  • 10
  • 77
  • 121
  • Like context.Response.BinaryWrite(fileBytes); context.ApplicationInstance.CompleteRequest(); ? – Salatiel Sep 25 '15 at 21:01
  • 1
    Alternatively, Response.End(). – Paul Kienitz Sep 25 '15 at 21:03
  • Also, if the filename might have spaces or commas or anything like that, you should put it in double quotes for the content-disposition header. – Paul Kienitz Sep 25 '15 at 21:04
  • Like that? context.Response.AppendHeader("content-disposition", "attachment; filename=\"" + sFileName + "\""); – Salatiel Sep 25 '15 at 21:09
  • And context.ApplicationInstance.CompleteRequest(); works like Response.End()? that methods do the same thing? – Salatiel Sep 25 '15 at 21:10
  • 1
    @Scbairros Yes, they are similar, but they are not the same. See [Is Response.End() considered harmful?](http://stackoverflow.com/questions/1087777/is-response-end-considered-harmful). – mason Sep 25 '15 at 21:18
  • context.ApplicationInstance.CompleteRequest(); doesn't works for me, but I resolved my problem. Thanks! – Salatiel Sep 29 '15 at 20:18
0

I resolved my problem using:

context.Response.Flush();
context.Response.Close();
context.Response.End();

at the end.

Salatiel
  • 121
  • 2
  • 10