目的是让应用程序的用户通过浏览器从Google云端硬盘下载图像(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))
}
您的情况也遇到了同样的问题。那时,起初,我将返回值转换为Unit8Array并将其转换为Blob。那么,在您的脚本中,如何进行以下修改?
const objectUrl = URL.createObjectURL(new Blob([response.body] , {type:'image/jpeg'}))
const objectUrl = URL.createObjectURL(new Blob([new Uint8Array(response.body.length).map((_, i) => response.body.charCodeAt(i))], {type: 'image/jpeg'}));
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句