I have this piece of code in a function:
Response.Clear();
Response.ContentType = "text/xml; charset=utf-8";
Response.AddHeader("Content-Disposition", "attachment; filename=test.xml");
// Put xml into response here
Response.End();
And this works, but, when it, it doesn't show a file size, in firefox it shows the filesize -1 and in chrome and ie it doesn't show a file size at all. How can I show the file size?
Did you try giving this:
Response.AddHeader("Content-Length", someBytes.Length.ToString());
If the content-length is set the web browser will show a progress bar while downloading. This is a very important usability feature for medium and large files, and you really want it. You want your user to know how far along they are, so they don't cancel the download and start it over, or worse just abandon your site. Refer
If your Response is of type System.Net.Http.HttpResponseMessage, you can insert a Content-Length header by using:
response.Content.Headers.ContentLength = <length in bytes>;
If you are stream a file your code could look like:
FileStream fileStream = File.Open("<source file name>", FileMode.Open);
HttpResponseMessage response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.OK;
response.Content = new StreamContent(fileStream);
response.Content.Headers.ContentLength = fileStream.Length;
response.Content.Headers.ContentType =
new MediaTypeHeaderValue("<media type e.g. application/zip>");
response.Content.Headers.ContentDisposition =
new ContentDispositionHeaderValue("file")
{
FileName = "<destination file name>"
};
return response;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With