下载zip文件并触发“保存文件"角度方法对话框 [英] Download zip file and trigger "save file" dialog from angular method
问题描述
我有一个标准的角度代码,该代码在单击按钮时触发:
I have a standard angular code, that is triggered when button is clicked:
$scope.downloadAll = function () {
$http.get("download.php?id=" + $scope.id).success(function (data) {
}).error(function () {
$scope.status = "Error: can not download table data from server.";
});
}
但是我的php脚本返回了一个zip存档,因此其内容最终显示在javascript变量 data 中.我想修改行为,以便浏览器提供zip存档以存储在本地磁盘上.保持角度按钮处理程序如何实现?
but my php script returns a zip archive and its contents thus ends up in javascript variable, data. I would like to modify the behaviour so that the zip archive is offered by browser to store on local disk. How I would achieve that, keeping the angular button handler?
推荐答案
在您的角度控制器中,注入$window
并像这样使用它:
In your angular controller, inject $window
and use it like this:
$scope.downloadAll = function () {
$window.location.href = "/download.php?id=" + $scope.id;
}
在服务器上,请确保您的zip文件带有适当的http标头,以提示文件下载(内容处置,mime类型等).
On the server, make sure that your zip file is served with the appropriate http headers to prompt a file download (content-disposition, mime type, ...).
这篇关于下载zip文件并触发“保存文件"角度方法对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!