[英]Google Drive API (Javascript) - Can't download pdf file content from Google Drive
尝试通过文件选取器从 Google Drive 下载 pdf 文件时遇到问题。 我的代码可以按预期处理图像和视频,但不能处理 pdf 文件。 通过API下载图片时https://www.googleapis.com/drive/v3/files/' + file.id + '?alt=media';
我得到以下回应:
name: "Social-Facebook-Post-Cool-Down.png"
lastModified: 1578397749873
lastModifiedDate: Tue Jan 07 2020 12:49:09 GMT+0100 (Central European Standard Time) {}
webkitRelativePath: ""
size: 317
type: ""
url: "blob:https://localhost:8080/647e4d15-d090-4c0f-aa3e-bd7481f11043"
但是当使用 pdf 文件 id 调用相同的 url 时,我没有 get url 属性,因此没有文件内容。 所以当打开下载的pdf文件时,它只是空的/空白的。 我在这里缺少什么?
这是我下载文件内容的代码:
for (let file of files) {
var xhr = new XMLHttpRequest();
var getUrl = 'https://www.googleapis.com/drive/v3/files/' + file.id + '?alt=media';
xhr.open("GET", getUrl, true);
xhr.setRequestHeader('Authorization', 'Bearer ' + this.googleDriveToken);
xhr.responseType = 'arraybuffer';
xhr.onload = () => {
var bytes = new Uint8Array(xhr.response);
let fileObj = new File([bytes], file.name);
googleFiles.push(fileObj);
counter++;
if (counter == files.length) {
this.$emit('filesAdded', googleFiles);
}
}
xhr.send();
}
谢谢。
正如 OP 所发现的,使用文件中的字节创建一个 blob 并将它们添加到 File 对象解决了这个问题:
var blob = new Blob([bytes]);
fileObj.url = blob;
参考Drive API 文档:
斑点
包含文本或二进制内容(例如图像、视频和 PDF)的文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.