Javascript 角度触发带文件名的api响应下载
我使用的是angular和laravel。在post请求中,PHP创建一个文件并用文件名响应。如何使用该文件名并触发从angular下载 api请求Javascript 角度触发带文件名的api响应下载,javascript,php,angularjs,Javascript,Php,Angularjs,我使用的是angular和laravel。在post请求中,PHP创建一个文件并用文件名响应。如何使用该文件名并触发从angular下载 api请求 var promise = $http.post('api/admin/ecs/singleecs', data). success(function (response) { //trigger file download }) php 为了解决这个问题,我将采取以下步骤: 在页面上创建
var promise = $http.post('api/admin/ecs/singleecs', data).
success(function (response) {
//trigger file download
})
php
为了解决这个问题,我将采取以下步骤:
<a id="fileDownloadLink" ng-hide="true" ng-href={downloadUrl} />
您可以使用
$window.location
触发下载。仅当“内容处置:附件”位于资源\u url的响应标题中时,此选项才有效:
var promise = $http.post('api/admin/ecs/singleecs', data)
.then(function (response) {
$window.location = response.resource_url;
});
注意:这是此处所示jQuery模式的角度版本:您的代码很有用,我需要修改它。现在我得到了url/home/abel/pro/public/sample.zip
,但是如果我点击它,它就会转到http://localhost:8000/home/abel/pro/public/sample.zip
如何删除localhost:8000它已经为laravel PHPC提供了服务。您能告诉我您的响应对象返回了什么吗?我正在返回创建的文件的路径,但大多数人不会阻止它吗浏览器作为弹出窗口?
$scope.downloadUrl = response.data.fileName;
var fileDownloadLink = document.getElementById("fileDownloadLink");
fileDownloadLink.target = "_blank"; // Opens the file in a new window.
fileDownloadLink.click();
var promise = $http.post('api/admin/ecs/singleecs', data)
.then(function (response) {
$window.location = response.resource_url;
});