WebAPI返回文件和下载没有启动

有一个非常奇怪的问题,当我返回一个文件作为“HttpResponseMessage”,它工作时,IAM返回PDF文件,但是当我返回一个XLS文件(Excel文件)它不。

这是响应标题,我试着将内容types改为“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文件时,看起来像是结果。

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

现在回到结果,不会开始下载…任何build议?


来自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. ***  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. *** 

UPDATE

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 

从新的方法,下载剂量开始。

 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类只包含字节[],contentLength,contentType和文件名

  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文件被下载到的客户端上检查MIMEtypes映射。

此外,如果您在应用程序/八位字节stream的响应中更改内容types,则客户端应该询问您是否要保存该文件 – 如果保存的文件已损坏,请进一步查看该文件的编码。

也看看Fiddler,以确保响应正确终止,我以前见过的文件发送正确,但其他邮件标记到响应stream结束由于服务器错误 – 在我的情况下,因为一个视图不存在时使用单轨 – 所以我们结束了一个Excel电子表格后500错误文本。

HTH,

缺口