WebAPI和angular JS Excel文件下载-文件损坏 [英] WebAPI and angular JS Excel file download - file corrupted
问题描述
我正在WebAPI中生成一个Excel文件.我将其存储"在内存流中,然后将响应如下:
I'm generating an Excel file in my WebAPI. I "store" it in a memorystream and then put in the the response as follow :
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;
服务器端似乎正常工作.如果我将该内存流写入文件流,则文件将被创建并且可以打开而没有任何问题.
It looks like the server side is working correcty. If I'm writing that memorystream into a filestream, the file is created and can be open without any problem.
从角度来看,单击按钮时如何重新创建文件?
On angular side, how can I recreate the file when click on a button?
我尝试过这样的事情:
$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();
})
}
文件已创建,但是当我尝试打开它时,它已损坏...我试图将数据类型更改为vnd.ms-excel,但它没有用... 点击该如何获取要下载的文件?
The file is created but when I tried to open it, it's corrupted... I've tried changing the data type to vnd.ms-excel in angular but it didn't work... How can I get the file to be downloaded on click?
编辑 在Jorg回答之后,我尝试了以下操作: api返回的是:
EDIT After Jorg answer, I tried the following : What the api returns is :
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
据我所见,它看起来是正确的.
From what I can see, it looks correct.
在客户端:
var file = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;' });
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
创建了一个excel文件,但是它仍然被破坏...
A excel file is created but it's still corrupted...
谢谢
推荐答案
我遇到了同样的问题.该文件在服务器端还可以,但是下载时已损坏.
I had the same problem. The file was OK server side, but when downloaded it became corrupt.
为我解决的是将responseType: "arraybuffer"
添加到服务器请求中.
What solved it for me was to add responseType: "arraybuffer"
to the server request.
然后在呼叫此线var file = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;' });
时
data
变量的类型应为ArrayBuffer
,而不是字符串.
Then when calling this line var file = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;' });
The data
variable should be of type ArrayBuffer
, and not a string.
这篇关于WebAPI和angular JS Excel文件下载-文件损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!