2015-09-23 58 views
0

我打電話給一個API並返回一個文件的字節數組,名稱,文檔類型和MIME類型,並嘗試添加Javascript來下載此文件。Javascript下載文件字節數組

我已將所有代碼連線並下載文件,但文件爲空。我使用下面的代碼:

result.success(function (data, status, headers, config) { 

      var arr = data.Document; 
      var byteArray = new Uint8Array(arr); 

      var a = window.document.createElement('a'); 

      a.href = window.URL.createObjectURL(new Blob([byteArray], { type: data.MimeType})); 
      a.download = agreementId+data.DocType; 

      // Append anchor to body. 
      document.body.appendChild(a); 
      a.click(); 


      // Remove anchor from body 
      document.body.removeChild(a); 
     }); 

我試過相同的代碼無需轉換Uint8Array但這只是打開與它的二進制數據,接近但也不能令人信服的文檔。例如,我得到一個包含以下文字文檔的下載: 0M8R4KGxGuEAAAAAAAAAAAAAAAAAAAAAPgADAP7/CQAGAAAAAAAAAAAAAAABAAAATwAAAAAAAAAAEAAAUQAAAAEAAAD + //// AAAAAE4AAAD ///////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// /////////////////////////// spcEAJ2AJBAAA

ect

回答