我正在尝试从AngularJS用$ http服务下载PDF文件。
当我尝试打开文件时,该文件无效(无效的色彩空间),页面空白。
然后我检查:
它们有所不同:即
“%PDF-1.4 %����.........
%PDF-1.4 %äãÏÒ.........
整个文档也是如此。
我在做什么错了?
P.S。代码:
downloadService.downloadPDF(response.url).then(
function(response) {
if (response) {
var aTag = document.getElementById('downloadFile');
aTag.setAttribute('href', 'data:application/pdf;charset=utf-8,' + encodeURIComponent(response));
aTag.setAttribute('download', $scope.data.filename);
aTag.click();
}
});
响应标题:
Cache-Control:max-age=3600, must-revalidate
Content-Disposition:inline; filename=4550f2025f45cc2d14a403fbc1840160d4d052e4_598551722_300501.pdf
Content-Type:application/pdf
Date:Tue, 23 Oct 2018 11:17:44 GMT
Expires:0
Pragma:public
Server:Apache-Coyote/1.1
Set-Cookie:JSESSIONID=Y0kTkAV7GJLVRK1fKuZfffff; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/appbase; HttpOnly
Transfer-Encoding:chunked
答案 0 :(得分:0)
encodeURIComponent不执行所需的BASE64编码
https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
在这里看看:
https://developer.mozilla.org/en-US/docs/Web/API/WindowBase64/Base64_encoding_and_decoding
或具有指向服务器上文件的链接