Error when forcing XML file download C #

0

I have a list of invoices where I can download XML, but there is a company server below the XML file that comes with the HTML of the page. The XML download code works normally on all servers where the application is installed except on that client. The method to download the file 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);           
    }

Any workaround to force the file to download?

    
asked by anonymous 25.09.2015 / 22:29

2 answers

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();
    
29.09.2015 / 22:09
1

I would only 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();

It's code that I use and always used when I want to download a file XML and it works perfectly ...

    
25.09.2015 / 23:12