亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

二進制文件損壞-如何下載帶有AngularJS的二進制文件

二進制文件損壞-如何下載帶有AngularJS的二進制文件

嗶嗶one 2019-07-02 14:35:12
二進制文件損壞-如何下載帶有AngularJS的二進制文件下載任何文件使用ResponseEntity與角不工作我需要下載一個文件使用角在客戶端,這個文件可以有任何格式,它可以是一個pdf或Excel,圖像或txt.我的方法只適用于txt文件,給出了Excel和圖像的失敗格式,而對于pdf,它給出了一個空的pdf。因此,在我的控制器中,有一個調用服務方法的函數:    vm.downloadFile = downloadFile;     function downloadFile(file){         var urlDir = "C://STCI//"+idpeticion;         return VerDocServices.downloadFile(file,urlDir)           .then(function(response) {             var data = response.data;             var filename = file;             var contentType = 'application/octet-stream';//octet-stream                          var linkElement = document.createElement('a');             try {                 var blob = new Blob([ data ], {                     type : contentType                });                 var url = window.URL.createObjectURL(blob);                 linkElement.setAttribute('href', url);                 linkElement.setAttribute("download", filename);                 var clickEvent = new MouseEvent("click", {                     "view" : window,                     "bubbles" : true,                     "cancelable" : false                 });                 linkElement.dispatchEvent(clickEvent);             } catch (ex) {                 console.log(ex);                 throw ex;             }         }).catch(function(response) {             alert('Se ha producido un error al exportar del documento');             console.log(response.status);             throw response;         });     }我的服務處有:angular.module('mecenzApp').service('VerDocServices',['$http',function($http) {this.downloadFile = function(file,urlDir) {     return $http.get('api/downloadFile', {         params : {             file : file,             urlDir : urlDir        }     }); }} ]);請你看一看,告訴我錯過了什么嗎?謝謝:)
查看完整描述

2 回答

?
小怪獸愛吃肉

TA貢獻1852條經驗 獲得超1個贊

如何使用AngularJS下載二進制文件

下載二進制文件時,設置responseType:

app.service('VerDocServices',['$http',function($http) {

    this.downloadFile = function(url, file, urlDir) {
        var config = {
            //SET responseType
            responseType: 'blob',
            params : {
                file : file,
                urlDir : urlDir            }
         };

        return $http.get(url, config)
          .then(function(response) {
            return response.data;
        }).catch(function(response) {
            console.log("ERROR: ", response.status);
            throw response;
        }); 
    }; }]);

如果responseType省略xhrapi默認為轉換。烏特夫-8編碼文本DOMString(UTF-16)這將破壞PDF、圖像和其他二進制文件。

有關更多信息,請參見MDN Web API引用-XHR響應類型


查看完整回答
反對 回復 2019-07-02
  • 2 回答
  • 0 關注
  • 713 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號