【问题标题】:how to download a file in angularjs controller如何在angularjs控制器中下载文件
【发布时间】:2015-09-07 11:03:37
【问题描述】:

我想在 angularjs 中单击按钮下载文件

 <button ng-click=downloadfile()>download</button>

在控制器中

  $scope.downloadfile=function()
        {
            //
        }

请您解释一下我必须在下载文件函数中编写哪些功能

【问题讨论】:

标签: angularjs


【解决方案1】:

在你的downloadFile() 函数中写出来。

    //Initialize file format you want csv or xls
    var uri = 'data:text/csv;charset=utf-8,' + escape(CSV);        

    //this trick will generate a temp <a /> tag
    var link = document.createElement("a");
    link.href = uri;

    //set the visibility hidden so it will not effect on your web-layout
    link.style = "visibility:hidden";
    link.download = fileName + ".csv"; //this is an example file to download, use yours

    //this part will append the anchor tag and remove it after automatic click
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);

【讨论】:

    【解决方案2】:

    $scope.downloadFile = 函数(文档){

            var token = $localStorage.authenticationToken || $sessionStorage.authenticationToken;
            if (token) {
                token = 'Bearer ' + token;
            }
    
            var url = new $window.URL($location.absUrl()).origin + "/api/voyages/" +
                vm.voyage.id + '/documents/' + doc.id + '?auth=' + token;
            $window.open(url);
        };
    

    【讨论】:

      猜你喜欢
      • 2016-06-18
      • 2019-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多