有一个非常奇怪的问题,当我将文件作为“HttpResponseMessage”返回时,它在iam返回pdf文件时有效,但是当我返回一个xls文件(excel文件)时,它没有。
以下是响应标头,我尝试将内容类型更改为“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. ***
更新
Iam使用post请求调用WEBAPI函数,并将其与旧响应结果进行比较,它们几乎相同。
我在这里发布了两个请求的respnose。
从旧方法开始下载...
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 start。
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
UPDATE添加方法,我返回结果 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);
}
}
答案 0 :(得分:1)
我会尝试仔细检查正在下载excel文件的客户端上的MIME类型映射。
此外,如果您更改application / octet-stream响应中的内容类型,客户端应该只询问您是否要保存文件 - 如果保存的文件已损坏,请进一步查看文件的编码。
同时查看Fiddler以确保响应被正确终止,我之前已经看到文件正确发送,但是由于服务器错误而将其他消息标记到响应流的末尾 - 在我的情况下因为视图没有'在使用Monorail时存在 - 所以我们最终得到了一个excel电子表格,后面跟着500个错误文本。
HTH,
尼克