我一直在尝试显示pdf文件,我从$http.post响应中获取blob .例如,pdf必须在应用程序中显示<embed src>.
我遇到了几个堆栈帖子,但不知怎的,我的例子似乎不起作用.
JS:
根据这份文件,我继续尝试......
$http.post('/postUrlHere',{myParams}).success(function (response) {
var file = new Blob([response], {type: 'application/pdf'});
var fileURL = URL.createObjectURL(file);
$scope.content = fileURL;
});
Run Code Online (Sandbox Code Playgroud)
根据我的理解,fileURL创建一个临时URL,博客可以将其用作参考.
HTML:
<embed src="{{content}}" width="200" height="200"></embed>
Run Code Online (Sandbox Code Playgroud)
我不知道如何在Angular中处理这个问题,理想的情况是(1)将它分配给范围,(2) '准备/重建'blob到pdf (3)将其传递给HTML使用<embed>因为我想要在应用程序中显示它.
我已经研究了一天以上但不知何故我似乎无法理解它在Angular中是如何工作的......让我们假设那里的pdf查看器库没有选项.
我根据斯科特的答案撰写了一份指令.你会这样使用它:
<button class="btn btn-success"
download-response="getData()"
download-success="getDataSuccess()"
download-error="getDataError()"
download-name="{{name}}.pdf"
download-backup-url="/Backup/File.pdf">
Save
</button>
Run Code Online (Sandbox Code Playgroud)
问题:下面的代码引发错误TypeError: Invalid calling object在IE11第一方法(行:saveBlob(blob, filename);).即使它回归到其他下载方法,我的理解是saveMethod1应该工作IE11.
这是代码:
'use strict';
// directive allows to provide a function to be executed to get data to be downloaded
// attributes:
// download-response - Required. Function to get data. It must return a promise. It must be declared on the $scope.
// download-success - Optional. Function to be executed if download-response function was …Run Code Online (Sandbox Code Playgroud)