скачать файл с угловым 6 и API-накопитель - PullRequest
0 голосов
/ 25 сентября 2019

У меня есть эта проблема, когда я пытаюсь загрузить некоторые файлы, работает только один файл .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 {Á¬�¸r��� ���ÉF! D50`��! À�A�! �H $ ´Ç9ÌñæÞê ~ ÝõªzfÌ = �Í {ÕU ·> î © [�] Õ = ��D "$H $ ��D" q�8 | þ ÷ ý '/

Ответы [ 2 ]

0 голосов
/ 25 сентября 2019

Я пытаюсь добавить 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()
0 голосов
/ 25 сентября 2019

вам нужно добавить это в ваш запрос

responseType: 'arraybuffer'

примерно так:

...
this.http.get(`${url}`,{
   responseType: 'arraybuffer', headers:headers}).subscribe(response => 
...
...