简体   繁体   中英

ASP.net MVC File Content-Length

The code below is a greatly simplified test action in a new MVC project. The action is meant to return a simple text file containing the word test .

public ActionResult FileTest()
{
    return File(System.Text.Encoding.UTF8.GetBytes("test"), "text/plain", "test.txt");
}

When viewing the HTTP response from the server I would expect to see a header with Content-Length: 4 , instead I see Content-Length: 122

Chrome and IE seem perfectly capable of coping with the miss match in Content-Length verses the number of bytes actually returned. Firefox (v47) however fails to download the file, indefinitely showing 4 of 122 bytes downloaded.

My question is: Is the behaviour I'm witnessing something I'm doing incorrectly, or is this a defect in the framework? Secondly, what I can do to resolve the issue?

I cross checked your code and it seems to be working fine on FireFox v47.

I suggest changing the return type from "ActionResult" to "FileResult" for returning files. This may resolve your issue. Let me know if you still face the same issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM