带角度6和api驱动器的下载文件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了带角度6和api驱动器的下载文件相关的知识,希望对你有一定的参考价值。
我尝试下载一些文件时遇到了这个问题,唯一可以正常工作的文件是.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 <...>] >>>
答案
您需要在请求中添加此内容
responseType: 'arraybuffer'
类似这样:
另一答案
我尝试添加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()
以上是关于带角度6和api驱动器的下载文件的主要内容,如果未能解决你的问题,请参考以下文章