我正在使用谷歌 API,我想從用戶界面下載檔案到我的谷歌驅動器。
正如我在此處的google drive API 檔案中發現的那樣,我想使用簡單的匯入。
目前我有這樣的代碼用于我的 onChange 輸入事件。
const onl oadFile = async (e: { target: { files: any } }) => {
const fileData = e.target.files[0];
//gapi request
uploadFile(body);
return null;
};
上傳檔案:
const uploadFile = async (body: string) => {
const result = await gapiRequest({
path: `${ENDPOINT}/upload/drive/v3/files`,
method: 'POST',
body,
});
setUploadFileData(result);
};
間隙請求:
const gapiRequest = async (options: gapi.client.RequestOptions): Promise<any> =>
new Promise<any>((resolve, reject) =>
gapi.client.request(options).execute((res) => {
resolve(res);
if (!res) {
reject(res);
}
})
);
我需要知道我需要為這樣的請求創建哪個請求正文。
uj5u.com熱心網友回復:
請求body應包含一個form同時包含元資料和檔案的檔案,如下所示:
const metadata = {
"name": "yourFilename",
"mimeType": "text/plain", // whatever is appropriate in your case
"parents": ["folder id or 'root'"], // Google Drive folder id
};
const form = new FormData();
form.append('metadata', new Blob([JSON.stringify(metadata)], { type: 'application/json' }));
form.append('file', file); // file could be a blob or similar
您可能還需要uploadType向您的path屬性添加一個引數。該multipart值甚至適用于簡單的上傳。
另見此處:https : //stackoverflow.com/a/68595887/7821823
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/318449.html
標籤:javascript 反应 文件 谷歌API 谷歌驱动器api
上一篇:linux_rename目錄中的檔案,通過使用find命令預先加上時間戳
下一篇:兩個函式在C中共享相同的簽名
