Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP.NET MVC 3中的HTTPContext响应中添加标题?_C#_Asp.net Mvc 3 - Fatal编程技术网

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

C# 如何在ASP.NET MVC 3中的HTTPContext响应中添加标题?,c#,asp.net-mvc-3,C#,Asp.net Mvc 3,我的页面中有一个下载链接,指向用户请求生成的文件。现在我想显示文件大小,这样浏览器就可以显示还有多少要下载。作为一个解决方案,我想在请求中添加一个头会起作用,但现在我不知道如何做 以下是我的试用代码: public FileStreamResult DownloadSignalRecord(长id、长powerPlantID、长generatinginitid) { SignalRepository sr=新的SignalRepository(); var file=sr.GetRecordFi

我的页面中有一个下载链接,指向用户请求生成的文件。现在我想显示文件大小,这样浏览器就可以显示还有多少要下载。作为一个解决方案,我想在请求中添加一个头会起作用,但现在我不知道如何做

以下是我的试用代码:

public FileStreamResult DownloadSignalRecord(长id、长powerPlantID、长generatinginitid)
{
SignalRepository sr=新的SignalRepository();
var file=sr.GetRecordFile(powerPlantID,generatingUnitID,id);
Stream=新内存流(文件);
HttpContext.Response.AddHeader(“Content-Length”,file.Length.ToString());
返回文件(流,“binary/RFX”,sr.GetRecordName(powerPlantID,generatingUnitID,id)+“.RFX”);
}

当我检查fiddler时,它没有显示内容长度标题。你们能帮帮我吗?

试试
HttpContext.Current.Response.AppendHeader(“Content-Length”,contentLength)

不确定还有什么错误,但内容长度应该是二进制文件的大小,而不是字符串长度。

请尝试以下代码,看看是否有效

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");
}
“它在我的机器上工作”


我尝试过不使用
内容长度
标题,Fiddler报告了一个内容长度标题。我认为不需要它。

这应该可以解决这个问题,因为我认为当您可以直接使用
字节[]
时,不需要使用
文件流结果

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");
}
请注意返回类型。

尝试使用

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

实际上,那里的长度应该是流的长度,也就是二进制文件的长度,对吗?HttpContext中不存在当前值。好的,请省略
Current
。只需在原始代码中使用
AppendHeader
而不是
AddHeader
。您的示例成功了,但当我生成文件时,它没有发生/您是否检查了流是否确实包含任何内容
sr.GetRecordFile(..)
返回一个
字节[]
,对吗?