【发布时间】:2021-06-09 07:49:38
【问题描述】:
目标是让应用用户通过浏览器从 Google Drive 下载图像 (jpg)。
使用更下方的代码,我确实设法让浏览器提示“另存为”对话框,并且用户将文件保存到磁盘,但是尝试打开 jpg 文件会导致错误:
“解释 JPEG 图像文件时出错(不是 jpeg 文件:以 0xc3 0xbf 开头)”
请问下面的代码有问题吗?
或者是因为响应有“content-encoding: gzip”,所以“response.body”需要解压缩?
const downloadFile = (fileId) => {
gapi.client.drive.files.get({
fileId: fileId,
alt: "media",
})
.then((response) => {
const objectUrl = URL.createObjectURL(new Blob([response.body] , {type:'image/jpeg'}))
// 'ref' is 'userRef' React hook pointing to an anchor element:
ref.current.href = objectUrl
ref.current.download = 'my-image.jpg'
ref.current.click()
})
.catch((err) => console.log(err))
}
【问题讨论】:
标签: javascript google-drive-api