Error forcing download XML file C#

Asked

Viewed 228 times

0

I have a list of tax notes where I can download XML, but there is a server of a company that below the XML file comes with the HTML of the page. The XML download code works normally on all servers where the application is installed except for this client. The file download method is as follows:

 public void StreamFileToBrowser(string sFileName, byte[] fileBytes, string extensao)
    {
        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}", extensao);
        context.Response.AppendHeader("content-disposition", "attachment; filename=" + sFileName);
        context.Response.BinaryWrite(fileBytes);           
    }

Some alternative solution to force file download?

2 answers

1

I would just use:

context.Response.OutputStream.Write(fileBytes, 0, fileBytes.Length);
context.Response.ContentType = "text/xml";
context.Response.AddHeader("Content-disposition", "inline; filename=o_meu_ficheiro.xml");
context.Response.End();

is code that I use and always used when I want to force the download of a file XML and works perfectly...

  • I will try to publish a version on the client server using the code you showed me, but it is calling all these functions precisely because of our attempts to solve the problem. Maybe it was like yours in the beginning. I picked up this code after others tried to solve it.

  • It didn’t work that way. Some other idea?

0


I was able to solve the problem by adding the following code at the end:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.