在我的Angular JS项目中,我有一个<a>锚标记,当单击它时会GET向返回文件的WebAPI方法发出HTTP 请求.
现在,我希望在请求成功后将文件下载到用户.我怎么做?
锚标记:
<a href="#" ng-click="getthefile()">Download img</a>
Run Code Online (Sandbox Code Playgroud)
AngularJS:
$scope.getthefile = function () {
$http({
method: 'GET',
cache: false,
url: $scope.appPath + 'CourseRegConfirm/getfile',
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
}).success(function (data, status) {
console.log(data); // Displays text data if the file is a text file, binary if it's an image
// What should I write here to download the file I receive from the WebAPI method?
}).error(function (data, status) {
// ...
});
}
Run Code Online (Sandbox Code Playgroud)
我的WebAPI方法: …
尝试了很多不同的方法后,没有任何方法可以帮助我.
最相似的问题是这一个如何在angularjs中阅读pdf流
我有一个AngularJS应用程序,它向我的PHP文件发送请求以生成PDF文件.我的控制器具有以下功能,我通过ng-click从按钮调用:
$scope.print = function() {
$http({
method : "post",
url : '/pdf/print_processor.php',
data : printdata,
//responseType : 'arraybuffer',
headers : {
'Content-type' : 'application/json'
}
}).success(function(response, status, headers) {
var file = new Blob([response], {type: 'application/pdf'});
var fileURL = URL.createObjectURL(file);
window.open(fileURL);
});
}
Run Code Online (Sandbox Code Playgroud)
主要原因是我必须将更多数据发送到PHP文件.printdata变量包含一个超过12 KB的嵌套JSON对象.所以我无法使用普通的GET或通过普通链接传递此数据作为URL的一部分.
请求正在运行,因为我可以直接调用print_processor.php(这给了我PDF),并且我使用相同的方法来创建发送电子邮件.就像上面提到的问题一样,我的回复也包含了pdf本身:
%PDF-1.5
3 0 obj
<</Type /Page
/Parent 1 0 R
/Resources 2 0 R
/Group <</Type /Group /S /Transparency /CS /DeviceRGB>>
/Contents 4 0 R>>
endobj
4 0 obj
<</Filter /FlateDecode …Run Code Online (Sandbox Code Playgroud)