简体   繁体   English

如何在 ASP.NET MVC 3 中的 HTTPContext 响应中添加标头?

[英]How to add Headers in HTTPContext Response in ASP.NET MVC 3?

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.作为一种解决方案,我想在请求中添加一个 Header 会起作用,但现在我不知道该怎么做。

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.当我检查提琴手时,它没有显示 Content-Length header。 Can you guys help me out?你们能帮帮我吗?

Try using尝试使用

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

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

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.而且我试过没有Content-length header 和提琴手报告内容长度 header 反正。 I don't think it's needed.我认为不需要。

This should solve it as I think there's no need to use the FileStreamResult , when you can use the byte[] directly.这应该可以解决它,因为我认为没有必要使用FileStreamResult ,当您可以直接使用byte[]时。

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.请注意FileContentResult返回类型。

Try using HttpContext.Current.Response.AppendHeader("Content-Length", contentLength);尝试使用HttpContext.Current.Response.AppendHeader("Content-Length", contentLength);

Not sure what else may be wrong there, but that content-length should be the size of the binary, not the string length.不确定那里可能还有什么问题,但内容长度应该是二进制文件的大小,而不是字符串长度。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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