Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.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和angular JS Excel文件下载-文件已损坏_C#_Angularjs_Excel_Rest_Asp.net Web Api - Fatal编程技术网

C# WebAPI和angular JS Excel文件下载-文件已损坏

C# WebAPI和angular JS Excel文件下载-文件已损坏,c#,angularjs,excel,rest,asp.net-web-api,C#,Angularjs,Excel,Rest,Asp.net Web Api,我正在WebAPI中生成一个Excel文件。我将其“存储”在memorystream中,然后将其放入响应中,如下所示: var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(ms) }; result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-str

我正在WebAPI中生成一个Excel文件。我将其“存储”在memorystream中,然后将其放入响应中,如下所示:

var result = new HttpResponseMessage(HttpStatusCode.OK) { Content = new StreamContent(ms) };

            result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = projectName + ".xlsx"
            };
           // ms.Close();
            return result;
看起来服务器端工作正常。如果我正在将memorystream写入一个文件流,那么该文件将被创建,并且可以毫无问题地打开

在角度方面,单击按钮时如何重新创建文件

我试过这样的方法:

$scope.exportQuotas = function (projectName) {
    homeService.GetQuotas(projectName, $routeParams.token, $scope.selection).then(
             function (data) {
                 var dataUrl = 'data:application/octet-stream;' + data
                 var link = document.createElement('a');
                 angular.element(link)
                   .attr('href', dataUrl)
                   .attr('download', "bl.xlsx")
                   .attr('target', '_blank')
                 link.click();
             })
}
该文件已创建,但当我试图打开它时,它已损坏。。。我尝试在angular中将数据类型更改为vnd.ms-excel,但没有成功。。。 如何在单击时下载文件

编辑 在Jorg回答之后,我尝试了以下方法: api返回的是:

Status Code: 200
Pragma: no-cache
Date: Tue, 02 Sep 2014 02:00:24 GMT
Server: Microsoft-IIS/8.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Content-Type: application/binary
Access-Control-Allow-Origin: *
Cache-Control: no-cache
X-SourceFiles: =?UTF-8?B?        QzpcVXNlcnNcdHJpaGFuaC5waGFtXFByb2plY3RzXFF1b3RhUXVlcnlcUXVvdGFRdWVyeUFQSVxhcGlccXVvdGFcR2V0?=
Content-Disposition: attachment; filename=O14Y0129AUG.xlsx
Content-Length: 13347
Expires: -1
从我看来,这是正确的

在客户端:

                 var file = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;' });
                 var fileURL = URL.createObjectURL(file);
                 window.open(fileURL);
excel文件已创建,但仍已损坏


谢谢,我也有同样的问题。该文件在服务器端正常,但下载时已损坏

对我来说,解决这个问题的方法是在服务器请求中添加
responseType:“arraybuffer”

然后,当调用此行时,{type:'application/vnd.openxmlformats of icedocument.spreadsheetml.sheet;'})
data
变量的类型应为
ArrayBuffer
,而不是字符串。

主要问题在于,默认情况下,XMLHttpRequest使用默认响应类型,即空字符串。看这个。因此,二进制数据没有得到正确处理

使用javascript API时,需要注意二进制数据的响应类型。否则,它将被解释为字符串


例如,类似于angular file upload(最新版本当前为1.1.5)的API不提供设置上传后接收的数据的响应类型的可能性。我必须更新API来定义响应类型。

我的API是这样工作的:但它对我不起作用:(文件已损坏…同样,文件名很奇怪…请提供关于如何添加响应类型的完整代码:“arraybuffer”到服务器请求?@Shivam657这是如何向请求添加响应类型的:
$http.get(url,{responseType:“arraybuffer”})
为我处理缓存:false,responseType:“arraybuffer”,savior@daniel非常感谢daniel我在过去两天里一直面临这个问题