1 回答

TA貢獻(xiàn)1875條經(jīng)驗(yàn) 獲得超3個(gè)贊
我相信你的目標(biāo)如下。
您想要使用 Javascript 從 Google Drive 下載 PDF 文件。
您
gapi.client
可用于下載該文件。
在這種情況下,我想建議修改用于從二進(jìn)制數(shù)據(jù)轉(zhuǎn)換為 blob 的腳本。
修改后的腳本:
一個(gè)簡(jiǎn)單修改的腳本如下。運(yùn)行此腳本時(shí),會(huì)下載 PDF 文件并將其作為文件保存到本地 PC。
gapi.client.drive.files
.get({
fileId: doc.id,
alt: 'media',
})
.then((res) => {
const filename = "sample.pdf";
// Convert binary data to blob.
const data = res.body;
const len = data.length;
const ar = new Uint8Array(len);
for (let i = 0; i < len; i++) {
ar[i] = data.charCodeAt(i);
}
const blob = new Blob([ar], {type: 'application/pdf'});
// Save the file.
const a = document.createElement('a');
document.body.appendChild(a);
const url = window.URL.createObjectURL(blob);
a.href = url;
a.download = filename;
a.click();
});
添加回答
舉報(bào)