20

I have a download link in my page, to a file I generate by the user request. Now I want to display the file size, so the browser can display how much is left to download. As a solution, I guess addin a Header to the request would work, but now I don't know how to do it.

Here is my try code:

public FileStreamResult DownloadSignalRecord(long id, long powerPlantID, long generatingUnitID)
{
    SignalRepository sr = new SignalRepository();
    var file = sr.GetRecordFile(powerPlantID, generatingUnitID, id);
    Stream stream = new MemoryStream(file);

    HttpContext.Response.AddHeader("Content-Length", file.Length.ToString());

    return File(stream, "binary/RFX", sr.GetRecordName(powerPlantID, generatingUnitID, id) + ".rfx");
}

When I checked on fiddler, it didn't display the Content-Length header. Can you guys help me out?

5 Answers 5

25

Try using

HttpContext.Response.Headers.Add("Content-Length", file.Length.ToString());
Sign up to request clarification or add additional context in comments.

Comments

18

Try HttpContext.Current.Response.AppendHeader("Content-Length", contentLength);

2 Comments

The Current doesn't exist inside HttpContext.
OK, then leave out Current. Just use AppendHeader instead of AddHeader in your original code.
4

Can you please try the following code and see if that works?

public FileStreamResult Index()
{
    HttpContext.Response.AddHeader("test", "val");
    var file = System.IO.File.Open(Server.MapPath("~/Web.config"), FileMode.Open);
    HttpContext.Response.AddHeader("Content-Length", file.Length.ToString());
    return File(file, "text", "Web.config");
}

"It works on my machine"

And I've tried without the Content-length header and Fiddler reports a content length header anyway. I don't think it's needed.

2 Comments

Your example worked, but when I generate my file, it doesn't happen. ;/
Have you checked that the stream actually contains anything? sr.GetRecordFile(..) returns a byte[], right?
1

This should solve it as I think there's no need to use the FileStreamResult, when you can use the byte[] directly.

public FileContentResult DownloadSignalRecord(long id, long powerPlantID, long generatingUnitID)
{
    SignalRepository sr = new SignalRepository();
    var file = sr.GetRecordFile(powerPlantID, generatingUnitID, id);

    HttpContext.Response.AddHeader("Content-Length", file.Length.ToString());

    return File(file, "binary/RFX", sr.GetRecordName(powerPlantID, generatingUnitID, id) + ".rfx");
}

Note the FileContentResult return type.

Comments

0

Not sure what else may be wrong there, but that content-length should be the size of the binary, not the string length.

1 Comment

Actually the length there should be the length of the stream, which would be the length of the binary, right?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.