gpt4 book ai didi

c# - WebAPI 返回文件和下载未开始

转载 作者:太空宇宙 更新时间:2023-11-03 13:31:23 24 4
gpt4 key购买 nike

有一个非常奇怪的问题,当我返回一个文件作为“HttpResponseMessage”时,它在我返回 pdf 文件时有效,但当我返回 xls 文件(excel 文件)时它不起作用。

这是响应 header ,我尝试将内容类型更改为“application/vnd.ms-excel”

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 316928
Content-Type: application/octet-stream
Content-Encoding: UTF-8
Expires: -1
Server: Microsoft-IIS/8.0
Content-Disposition: attachment; filename=f.xls
Content-Description: File transfer
Charset: UTF-8
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcZGV2XHRmc1xrYmFzcnZcZUdhaW4uV2Vic2l0ZXMuRm9yZWNhc3RpbmdcYXBpXFByaW50XENhbGxpbmdMaXN0QnlDb21wYW55T3JDdXNvdG1lcklk?=
X-Powered-By: ASP.NET
Date: Fri, 06 Dec 2013 07:49:47 GMT

当您在记事本中打开 xls 文件时,我得到的结果看起来像。

我发现了一个非常奇怪的解决方案,即将其转换为 base64 并链接到“data: application/octet-stream; base64,”这将开始下载你不知道文件的名称所以用户不会理解将它保存为 XLS 文件...

现在我回到了无法开始下载的结果...有什么建议吗?


这是来自 fiddler2 的响应 Raw

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 316928
Content-Type: application/octet-stream
Expires: -1
Server: Microsoft-IIS/8.0
Set-Cookie:
path=/; HttpOnly
Content-Disposition: attachment; filename=f.xls
Content-Description: File transfer
Charset: UTF-8
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcZGV2XHRmc1xrYmFzcnZcZUdhaW4uV2Vic2l0ZXMuRm9yZWNhc3RpbmdcYXBpXFByaW50XENhbGxpbmdMaXN0QnlDb21wYW55T3JDdXNvdG1lcklk?=
X-Powered-By: ASP.NET
Date: Fri, 06 Dec 2013 08:40:51 GMT

��ࡱ�����������������>���� ���������������d�������������������������e��f��g��h��i������������������������������������

*** FIDDLER: RawDisplay truncated at 128 characters. Right-click to disable truncation. ***

更新

我是用post请求调用WEBAPI函数,和旧的response结果对比,几乎一样。

我在这里发布两个请求的响应。

从旧方法开始下载...

HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/vnd.ms-excel; charset=utf-8
Server: Microsoft-IIS/8.0
Content-Disposition: attachment; filename=CallList_2013-12-06.xls
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcZGV2XHRmc1xrYmFzcnZcZUdhaW4uV2Vic2l0ZXMuRm9yZWNhc3RpbmdcZUNSTVxDYWxsTGlzdEV4cG9ydC5hc3B4?=
X-Powered-By: ASP.NET
Date: Fri, 06 Dec 2013 10:11:13 GMT
Content-Length: 34590

从新方法开始,下载dosent。

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 316928
Content-Type: application/vnd.ms-excel; charset=utf-8
Content-Encoding: UTF-8
Expires: -1
Server: Microsoft-IIS/8.0
Content-Disposition: attachment; filename=CallList_2013-12-06.xls; charset=utf-8
Charset: UTF-8
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcZGV2XHRmc1xrYmFzcnZcZUdhaW4uV2Vic2l0ZXMuRm9yZWNhc3RpbmdcYXBpXFByaW50XENhbGxpbmdMaXN0QnlDb21wYW55T3JDdXNvdG1lcklk?=
X-Powered-By: ASP.NET
Date: Fri, 06 Dec 2013 11:04:49 GMT

更新添加了我返回结果的方法ByteResult 类只包含 byte[]、contentLength、contentType 和 filename

 protected HttpResponseMessage ByteResult(ByteResult result)
{
try
{


var responseStream = new MemoryStream();
var buffer = result.Data;
responseStream.Write(buffer, 0, Convert.ToInt32(result.ContentLenght));

// No Range header. Return the complete file.
responseStream.Position = 0;

var response = new HttpResponseMessage
{
StatusCode = HttpStatusCode.OK,
Content = new StreamContent(responseStream)
};


response.Content.Headers.Add("Content-Disposition", "attchment; filename=" + HttpUtility.UrlEncode(result.Name, Encoding.UTF8));
response.Content.Headers.Add("Content-Type", result.ContentType );
response.Content.Headers.Add("Content-Description", "File Download");
response.Content.Headers.Add("Content-Encoding", "UTF-8");
response.Content.Headers.Add("Charset", "UTF-8");
response.Content.Headers.Expires = new DateTimeOffset(DateTime.Now);


return response;
}
catch (IOException ioex)
{
return ErrorResult(ioex, "Unknown IO Error", HttpStatusCode.InternalServerError);
}
catch (Exception ex)
{

return ErrorResult(ex, "Unknown Error", HttpStatusCode.InternalServerError);
}


}

最佳答案

我会尝试在将 excel 文件下载到的客户端上仔细检查 MIME 类型映射。

此外,如果您在对 application/octet-stream 的响应中更改内容类型,客户端应该只询问您是否要保存文件 - 如果保存的文件随后损坏,请进一步查看文件的编码.

还要查看 Fiddler 以确保正确终止响应,我之前看到文件已正确发送,但由于服务器错误,其他消息被标记到响应流的末尾 - 在我的情况下,因为 View 没有'使用 Monorail 时不存在 - 所以我们最终得到了一个 excel 电子表格,后跟 500 个错误文本。

HTH,

尼克

关于c# - WebAPI 返回文件和下载未开始,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20418883/

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