Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/25.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# 返回文件和下载的WebAPI未启动_C#_Excel_Download_Httpresponse_Asp.net Web Api - Fatal编程技术网

C# 返回文件和下载的WebAPI未启动

C# 返回文件和下载的WebAPI未启动,c#,excel,download,httpresponse,asp.net-web-api,C#,Excel,Download,Httpresponse,Asp.net Web Api,有一个非常奇怪的问题,当我返回一个文件作为“HttpResponseMessage”时,它在我返回pdf文件时起作用,但当我返回一个xls文件(excel文件)时它不起作用 这是响应标题,我已尝试将内容类型更改为“application/vnd.ms excel” 我得到的结果与在记事本中打开xls文件时的结果类似 我提出了一个非常奇怪的解决方案,将其转换为base64并链接到“data:application/octet stream;base64,” 如果您不知道文件名,那么用户就不知道如何

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

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

我得到的结果与在记事本中打开xls文件时的结果类似

我提出了一个非常奇怪的解决方案,将其转换为base64并链接到“data:application/octet stream;base64,” 如果您不知道文件名,那么用户就不知道如何将其保存为XLS文件,这将启动下载

现在我回到了不会开始下载的结果。。。有什么建议吗


下面是来自Fiddler 2的原始响应

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函数,并将其与旧的响应结果进行比较,结果几乎相同

我在这里发布两个请求的回复

从旧方法开始,下载开始

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
更新我返回结果的添加方法 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文件的客户端上再次检查MIME类型映射

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

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

嗯,


Nick

尝试将内容类型设置为excel类型。我已经尝试过了。您是否使用其他浏览器尝试过此操作?还是另一种文件类型?可能只是你的浏览器对如何处理xls文件感到困惑。我尝试过firefox和chrome,尝试过将类型更改为xlsx,嗯,尝试将其更改为open office格式…,当你返回八位字节流结果时,类型无关紧要。你能共享生成此响应的webapi代码吗?我将检查映射=),我曾经尝试过FF和CHROME,我认为将内容类型设置为application/octet stream会让文件下载,这就是为什么会出现这种恼人的问题。
 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);
            }


        }