2017-08-23 53 views
0

這是以角度2下載文件的代碼。我想用此代碼下載pdf,txt,docx,excel文件。我使用了文件保護程序,因此我們可以通過單擊按鈕下載不同的文件。如何在角度2下載多個文件

public downloadCSVFile() 
    { 
     this.downloadPdf().subscribe(
     (res) => 
     { 
      //console.log(res); 
      saveAs(res,'Frontend.xlsx') 
     } 
    ); 
    } 

    public downloadPdf(): any 
    { 
     let urls='assets/Files/Frontend.xlsx' 
     let headers = new Headers(); 
     //headers.append('Authorization', 'JWT ' + localStorage.getItem('id_token')); 
     return this.http.get(urls, { headers: headers,responseType: ResponseContentType.Blob }).map(
     (res) => { 
      return new Blob([res.blob()], { type: 'application/vnd.ms-excel' }) 
     }) 
    } 

這是我的html代碼所以如何下載diffrent文件。由於文件名顯示在表中,所以我想要下載相應的文件。

<tr *ngFor ="let mydrive of providerMyDrive"> 
     <td>{{ mydrive.name }}</td> 
     <td>{{ mydrive.filetype }}</td> 
     <td>{{ mydrive.size }}</td> 
     <td>{{ mydrive.uploaddate }}</td> 
     <td><input type="button" (click)="downloadCSVFile()" value="Download"></td> 
     <td></td> 
     </tr> 

回答

0

只需創建一個鏈接並點擊:

this.storageService.downloadFile(path).subscribe(
     res => { 
     let link = document.createElement('a'); 
     link.href = window.URL.createObjectURL(res.blob()); 
     link.download = path; 

     link.click(); 
     }, error => this.globalService.errorSubject.next(error) 
    );