我正在尝试从Google Drive导出docx文件。
gapi.client.drive.files.export({ fileId: id, alt: 'media', mimeType: _mimeType }).then((_response) => {
var fileType = _response.headers['content-type'];
var base64 = new Buffer(_response.body, 'utf8').toString('base64');
var dataURI = 'data:' + fileType + ';base64,' + base64;
我把它寄给
https://content.googleapis.com/drive/v3/files
:
mimeType: application/vnd.openxmlformats- officedocument.wordprocessingml.document
alt: media
key: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
例如,它适用于图像,但Word显示
The file is corrupt and cannot be open
.
我觉得有问题
base64
.
我如何解决?
更新:
我试图打开一个关于fe的文件,以确保它不是be s问题:
var reader = new FileReader();
var out = new Blob([_response.body], { type: "application/vnd.openxmlformats-officedocument.wordprocessingml.document" });
reader.onload = function (e) {
window.location.href = reader.result;
}
reader.readAsDataURL(out);
结果-
文件已损坏,无法打开
我的最终解决方案:
var xhr = new XMLHttpRequest();
xhr.open("GET", "https://content.googleapis.com/drive/v3/files/" + id + '/export?alt=media&mimeType=' + _mimeType, true);
xhr.setRequestHeader('Authorization', 'Bearer ' + this.access_token);
xhr.responseType = 'arraybuffer'
xhr.onload = () => {
var base64 = 'data:' + _mimeType +';base64,' + this.base64ArrayBuffer(xhr.response);
resolve(base64);
}
xhr.send();
base64ArrayBuffer
来自
https://gist.github.com/jonleighton/958841