gpt4 book ai didi

asp.net - HTTP 响应行为

转载 作者:可可西里 更新时间:2023-11-01 16:39:35 26 4
gpt4 key购买 nike

我有两段非常相似的 ASP.NET 代码,它们在 HTTP 响应中向客户端发送文件。它们应该使浏览器提示保存文件。第一个有效,第二个无效。在 Fiddler 中看到的 HTTP 响应如下。

工作:

HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 228108
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 4.0.30319
content-disposition: attachment; filename=Report.xlsx
Date: Wed, 05 Jan 2011 12:17:48 GMT
<binary data>

不工作:

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Wed, 05 Jan 2011 12:19:21 GMT
X-AspNet-Version: 4.0.30319
Content-Length: 228080
content-disposition: attachment; filename=report 2.xlsx
Cache-Control: private
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Connection: Close
<binary data>

当在 Fiddler 中看到第一个文件时,浏览器会正确提示保存文件。当在 Fiddler 中看到第二个时,浏览器中没有任何可观察到的事情发生。 chrome 和 firefox 中的行为相同。

知道为什么会这样吗?

编辑:产生第二个响应的 ASP.NET 代码

Response.Buffer = false;
Response.ContentType = @"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AppendHeader("content-length", genstream.Length.ToString());
Response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}.xlsx", filename));

byte[] buffer = new byte[1024];
genstream.Position = 0;

int n;
while ((n = genstream.Read(buffer, 0, 1024) ) > 0)
{
Response.OutputStream.Write(buffer, 0, n);
}

最佳答案

filename 参数值中的空格可能会导致这种情况。试试 quoted-string syntax相反:

Content-Disposition: attachment; filename="report\ 2.xlsx"

另见 RFC 2183 .

关于asp.net - HTTP 响应行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4604156/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com