使用angularJS中的Blob方法破坏下载的文档
发布时间:2020-12-17 07:36:17 所属栏目:安全 来源:网络整理
导读:下载一个曾经在我的应用程序中正常工作的文件,直到我将Angular升级到最新版本.即使是现在,该文件也已下载,但问题是它已损坏.上传文件工作正常,如果我们签入文件服务器,文件将完好无损.但下载后,我的文件已损坏. Html: td data-title="''" a tooltip="Downlo
|
下载一个曾经在我的应用程序中正常工作的文件,直到我将Angular升级到最新版本.即使是现在,该文件也已下载,但问题是它已损坏.上传文件工作正常,如果我们签入文件服务器,文件将完好无损.但下载后,我的文件已损坏.
Html: <td data-title="''">
<a tooltip="Download CV" ng-hide="!talent.resumePath" tooltip-trigger tooltip-animation="false" tooltip-placement="bottom" ng-click="downloadResume(talent.id)" data-placement="top" data-toggle="tooltip" data-original-title="resume">
<img src="../../img/DownloadIcon.png" /></a>
</td>
控制器: downloadResume: function(employeeId) {
return apiServices.getFileFromTalentPool('/talentpool/resume?id=' + employeeId)
},
其中,getFileFromTalentPool是:https://hastebin.com/yivaterozi.js 终点: public FileResult GetResume(int id) {
var result = _services.GetResume(id);
if (result != null) {
HttpContext.Response.ContentType = result.ContentType;
HttpContext.Response.Headers["Access-Control-Expose-Headers"] = "FileName";
HttpContext.Response.Headers["FileName"] = result.FileDownloadName;
}
return result;
}
通常我下载Doc文件.我尝试使用记事本文件来查看它是否相同.奇怪的是,我注意到我能够打开记事本文件,但其内容被操作为[object Object].但对于Doc文件,它只显示http://oi68.tinypic.com/2i11m9y.jpg 我怎样才能解决这个问题?
看起来
https://hastebin.com/yivaterozi.js的代码已经从使用不推荐的$http.success()方法更新为当前的$http.then(). Promise’成功回调函数(在then方法中)只接收一个对象参数:
https://docs.angularjs.org/api/ng/service/ $http.不成功的“成功”方法获得了更多参数(数据,状态,标题)和已包含原始数据的数据.使用then()时,数据位于response的data属性下,因此请尝试将$http调用更改为:
$http({
method: 'GET',cache: false,url: fileurl,responseType:'arraybuffer',headers: {
'Authorization': "Bearer " + $rootScope.userInfo.access_token,'Access-Control-Allow-Origin': '*'
}
}).then(function (data) {
var octetStreamMime = 'application/octet-stream';
var success = false;
// Get the headers
var headers = data.headers();
...
...
请注意,从数据对象而不是第三个参数中获取正确的标题(只需添加var,因为我们删除了空参数). // Try using msSaveBlob if supported
var blob = new Blob([data.data],{ type: contentType });
或者只是将参数数据更改为响应并添加var data = response.data; anf修改header getter to headers = response.headers();: $http({
method: 'GET','Access-Control-Allow-Origin': '*'
}
}).then(function (response) {
var octetStreamMime = 'application/octet-stream';
var success = false;
// Get data
var data = response.data;
// Get the headers
var headers = response.headers();
...
...
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读
