在Angular中是否可以通过NG-CLICK或NG-SUBMIT从服务器下载文件? (使用PHP)

时间:2014-05-28 12:53:42

标签: php angularjs post

现在,我的功能看起来像这样

    scope.downloadCsv = function(){
         $http.({
          method:'POST',
          url:'php/crud.php',
          data:scope.postPayload,
          headers:{'content-type':'application/x-www-form-urlencoded'} 
          }).success(function(){console.log("download done!")});
     };

我已尝试将该按钮绑定到ng-click,ng-submit,并按照Download text/csv content as files from server in Angular上的说明进行操作(有效,但不适用于Firefox)。

我不知道技术术语,但我假设我需要能够点击一个带有" POST"的链接。请求,浏览器认为它不是ASYNC电话,我只是不知道如何完成它。它必须是一个帖子,因为我将一堆数据传递给服务器以进行操作以生成CSV。任何帮助都非常值得赞赏!

1 个答案:

答案 0 :(得分:2)

您无法使用ajax下载文件。我的第一选择是创建一个普通的html表单,发布到隐藏的iframe:

<form method="post" action="php/crud.php" target="hidden_iframe">
<input type="hidden" name="data" value="{{postPayload}}">
<button type="submit">Download!</button>
</form>
<iframe name="hidden_iframe"></iframe>