2016-11-04 60 views
4

我試圖在Google Drive上使用ArrayBuffer的內容創建文件。我正在使用Javascript和版本3的Drive aPI。使用Google Drive API v3和Javascript將ArrayBuffer的內容上傳到Google雲端硬盤

我設法使用this code sample創建了一個填充文本文件,但是我找不到任何有關如何使用來自ArrayBuffer的二進制數據填充文件的示例。這是可能的,還是我必須將我的二進制數據編碼爲字符串,然後才能將其上傳到Google Drive?

谷歌的文檔,V3真是個缺乏:-(

任何幫助,高度讚賞。

+0

這可能有所幫助:https://github.com/googledrive/web-quickeditor/blob/master/src/components/drive/multipart.js使用V3上傳文件不應該與v2不同。我同意V3還沒有太多的文檔。 – DaImTo

+0

@DaImTo感謝您的鏈接。你能指出我正確的方向:如何使用此代碼更新來自ArrayBuffer的數據的文件? – DataGrump

回答

0

我掙扎,這也並能得到它的base64ing基於UTF-8字符串表示工作該ArrayBuffer的它看起來是這樣的:

// file.data is an ArrayBuffer 
var b64str = btoa(String.fromCharCode.apply(null, new Uint8Array(file.data))); 

然後,您需要在指定的驅動器API的多文件上傳的內容傳送編碼來了解文件:

// 'data' is our base64'd image that came from ArrayBuffer 
var requestBody = 
    '--' + boundary_string + '\n' + 
    "Content-Type: application/json; charset=UTF-8\n\n" + 
    JSON.stringify(metadata) + '\n' + 
    '--' + boundary_string + '\n' + 
    "Content-Type: " + mime + "\n" + 
    "Content-Transfer-Encoding: base64" + 
    data + '\n' + 
    '--' + boundary_string + '--'; 
相關問題