带角度6和api驱动器的下载文件

问题描述 投票:0回答:2

我尝试下载一些文件时遇到了这个问题,唯一可以正常工作的文件是.txt其他文件已损坏。

  async downloadFromDrive(accountId: string, fileName: string, partNumber): Promise<boolean> {
return new Promise((resolve,reject)=>{


 gapi.load('client:auth2', () => {
return gapi.client.init({
  apiKey: this.API_KEY,
  clientId: this.CLIENT_ID,
  discoveryDocs: this.DISCOVERY_DOCS,
  scope: this.SCOPES
}).then(() => {
  this.googleAuth = gapi.auth2.getAuthInstance();
  this.googleAuth.signIn().then(() => {
    gapi.client.setToken({access_token: this.cookieService.get(accountId)});
    gapi.client.drive.files.get({
      fileId: fileName,
      alt: 'media',
    }).then(res => {
      let blob = new Blob([res.body], {type: 'application/pdf'
      });
      this.sortedFiles[partNumber] = blob;
      console.log('res-body',res.body)
      resolve()
    }) 
  })
});` });

LOG:

âãÏ3 0 obj <>流x�í]M��E�}¿DÁÿ�­ú�����MÜ»0 {Á¬¬rr������ÉF!d50`�� !à�A�!¡

我尝试下载某些文件时遇到了这个问题,唯一可以正常工作的文件是.txt。其他文件已损坏。异步downloadFromDrive(accountId:字符串,fileName:字符串,partNumber):Promise <...>] >>>

angular drive
2个回答
0
投票

您需要在请求中添加此内容

responseType: 'arraybuffer'

类似这样:


0
投票

我尝试添加responseType,但是文件始终损坏:

async downloadFromDrive(accountId: string, fileName: string, partNumber): Promise<boolean> {
return new Promise((resolve,reject)=>{
 gapi.load('client:auth2', () => {
return gapi.client.init({
  apiKey: this.API_KEY,
  clientId: this.CLIENT_ID,
  discoveryDocs: this.DISCOVERY_DOCS,
  scope: this.SCOPES
}).then(() => {
  this.googleAuth = gapi.auth2.getAuthInstance();
  this.googleAuth.signIn().then(() => {
    gapi.client.setToken({access_token: this.cookieService.get(accountId)});
    gapi.client.drive.files.get({
      fileId: fileName,
      /* alt: 'media'*/
      alt: 'media',


    /* mimeType:'image/jpeg'*/
    }).then(res => {

       let header= new HttpHeaders({
         'Authorization' : 'Bearer ' +  this.cookieService.get(accountId),
         'responseType' : 'arraybuffer'

       });

       this.http.get(res.result.webContentLink, { responseType: 'arraybuffer', headers: header} ).subscribe(res => {console.log(res);
       /*resolve()*/});
     console.log('download',res)
      let blob: Blob = new Blob([res.body],{ type:'mimeType'})
      this.sortedFiles[partNumber] = blob;
      console.log('res-body')
      resolve()
© www.soinside.com 2019 - 2024. All rights reserved.