第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Google Drive Web API 保存并下載 pdf

Google Drive Web API 保存并下載 pdf

慕運(yùn)維8079593 2023-07-20 14:51:05
我試圖從 google 驅(qū)動(dòng)器獲取 pdf 并在瀏覽器中下載它(我實(shí)際上需要它作為數(shù)組緩沖區(qū)或 blob 來與 pspdfkit 一起使用,但這劑量相同,并且描述起來更簡(jiǎn)單)。到目前為止,我嘗試了很多方法,但無法從 api 得到的響應(yīng)中獲取 pdf 文件。我想指出這是 Angular 10 應(yīng)用程序的一部分,但我認(rèn)為它不相關(guān)。另外我需要指定我正在使用打字稿,但我也做了一個(gè) js 版本來測(cè)試,同樣似乎產(chǎn)生相同的結(jié)果。該doc對(duì)象來自谷歌驅(qū)動(dòng)器選擇器google.picker.PickerBuilder這是我用來獲取文件內(nèi)容的代碼gapi.client.drive.files .get({  fileId: doc.id,  alt: 'media',  // mimeType: 'application/pdf', // <- with or without  // responseType: 'arraybuffer', // <- with or without }) .then((res) => {  // manipulate res.body  window.resb=res.body;  // HERE <- I continue to call saveFile and debug the response });這是我用來測(cè)試是否可以將響應(yīng)用作 pdf 文件的函數(shù):function saveFile(blob, filename) {  if (window.navigator.msSaveOrOpenBlob) {    window.navigator.msSaveOrOpenBlob(blob, filename);  } else {    const a = document.createElement('a');    document.body.appendChild(a);    const url = window.URL.createObjectURL(blob);    a.href = url;    a.download = filename;    a.click();    setTimeout(() => {      window.URL.revokeObjectURL(url);      document.body.removeChild(a);    }, 0)  }}這是我嘗試將字符串解析為數(shù)組緩沖區(qū)的另一個(gè)函數(shù)function str2ab(str) {  var buf = new ArrayBuffer(str.length*2); // 2 bytes for each char  var bufView = new Uint16Array(buf);  for (var i=0, strLen=str.length; i < strLen; i++) {    bufView[i] = str.charCodeAt(i);  }  return buf;}這就是我實(shí)際調(diào)用該download函數(shù)的方式saveFile(new Blob([resb],{type: 'application/pdf',}),'a.pdf'); // <- only one that actually produces an openable pdf file, but empty或者saveFile(new Blob([str2ab(resb)],{type: 'application/pdf',}),'a.pdf');或者saveFile(new Blob(str2ab(resb),{type: 'application/pdf',}),'a.pdf');...以及其他一些方法。我沒有什么想法,而且我現(xiàn)在處于a (618).pdf。請(qǐng)幫忙 :)
查看完整描述

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();

 });


查看完整回答
反對(duì) 回復(fù) 2023-07-20
  • 1 回答
  • 0 關(guān)注
  • 224 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)