Google Drive API压缩文件已损坏

时间:2019-03-12 04:52:37

标签: javascript drive

由于从Google Drive REST API下载后损坏,因此无法打开zip文件。 代码如下:

gapi.client.drive.files.get({'fileId':id,alt:'media'}).then((res)=>{
   const zipblob = new Blob([res.body])
   let a = document.createElement('a')
   a.download = 'sample.zip'
   a.href = window.URL.createObjectURL(zipblob)
   a.click()
}

Drive REST API的响应标头如下:

Content-Type: "application/zip"
cache-control: "private, max-age=0, must-revalidate"
content-length: "216"
content-type: "text/plain;charset=UTF-8"
date: "Tue, 12 Mar 2019 04:40:39 GMT"
expires: "Tue, 12 Mar 2019 04:40:39 GMT"
server: "UploadServer"
vary: "Origin, X-Origin"
x-guploader-uploadid: "AEnB2Upp9nbCpCrydypTCBqEH-Yc8p7cpRbvDLBGyxb9t4n1TRGux7Zcvw_2fSd3ibEav2q4cw6luDRoMNwzc5ytX57LsEwHbg"

我在某处找到了解决方案,并且可以正常工作。

function str2bytes(str){
   var bytes = new Uint8Array(str.length)
   for (var i=0; i<str.length; i++){
      bytes[i] = str.charCodeAt(i);
   }
   return bytes;
}

gapi.client.drive.files.get({'fileId':id,alt:'media'}).then((res)=>{
   const zipblob = new Blob([str2bytes(res.body)])
   let a = document.createElement('a')
   a.download = 'sample.zip'
   a.href = window.URL.createObjectURL(zipblob)
   a.click()
})

0 个答案:

没有答案