我有一個真的很難的時間解決這個問題。我嘗試了很多不同的解決方案,我不知道現在需要修復的地方。我嘗試通過http獲取Blob並使用FileSaver.js爲用戶下載文件。由於某種原因,每次我嘗試打開圖像時,都會收到「已損壞,已損壞或過大」消息。我試圖玩'responseType'(改爲'blob'),在頭上添加一個'Accept'。沒有什麼似乎爲我工作!使用AngularJS和Angular-FileSaver.js下載Blob通過http
有人可能會指出我在正確的方向嗎?
服務
download: function(blobId, token) {
var req = {
method: 'GET',
cache: false,
url: 'api/Blob/DownloadBlob/' + blobId,
headers: {
'responseType': 'arraybuffer',
'Authorization': token
}
};
return $http(req)
.then(function (response) {
return response;
}, function(response) {
// something went wrong
return $q.reject(response.data);
});
}
控制器
$scope.downloadFile = function() {
Data.download($scope.blobId, $scope.token).then(function (response) {
var blob = new Blob([response], { type: 'image/png' });
FileSaver.saveAs(blob, 'download.png');
});
};
'responseType'是** **不是一個頭。請參閱https://docs.angularjs.org/api/ng/service/$http#usage – Phil
FileSaver不適用於Safari或iOS Safari瀏覽器。我相信它應該可以在Mac上的Safari 10.1上運行,因爲該版本表示它支持anchor標記中的下載屬性(文件引擎使用什麼文件) – Patrick