久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Google Drive API V3 Javascript - 創建包含內容的文件

Google Drive API V3 Javascript - Create File with Content(Google Drive API V3 Javascript - 創建包含內容的文件)
本文介紹了Google Drive API V3 Javascript - 創建包含內容的文件的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

之前有人問過這個問題,但答案是使用 API V2.谷歌文檔沒有說明如何使用 javascript 客戶端代碼創建包含其內容的文件.我嘗試使用節點下列出的代碼,但是,它只創建文件,不插入任何內容.這是我的代碼:

This question has been asked before, but the answer was using API V2. The google documentation does not clarify how to create a file with its content using javascript client code. I tried using the code listed under Node, however, it only creates the file, it does not insert any content. Here is my code:

  let fileMetadata = {
    'name': name,
    parents: [parentId]
  };

  let media = {
    mimeType: 'text/plain',
    body: 'content inside file'
  };

  gapi.client.drive.files.create({
    resource: fileMetadata,
    media,
    fields: 'id'
  })
  .then(response => {
    console.log('response: ', response);
  })
  .catch(() => {
    console.log('something is wrong');
  });

有人可以幫我將內容插入文件嗎?

Can someone help me insert content into files please?

推薦答案

這個示例腳本怎么樣?在我的環境中,雖然 gapi.client.drive.files.create() 可以在 Google Drive 上創建一個空文件,但它不能直接上傳包含內容的文件.我認為這可能無法使用 multipart/related 上傳文件和元數據,盡管這可能會在未來的更新中得到解決.所以現在,作為一種解決方法,我使用 XMLHttpRequest.

How about this sample script? In my environment, although gapi.client.drive.files.create() can create an empty file on Google Drive, it cannot directly upload files including contents. I think that this might not be able to upload files and metadata with the multipart/related, although this might be resolved by the future update. So now, as one of workarounds, I use XMLHttpRequest.

在使用此示例腳本之前,請確認以下幾點.

Before you use this sample script, please confirm the following points.

  • 在您的情況下,您已經能夠使用 gapi 創建文件.在我的腳本中,訪問令牌是使用 gapi 檢索的.
  • 使用此腳本時,請設置文件內容和元數據.

在此示例腳本中,在文件夾下創建了一個包含內容的文本文件.

In this sample script, a text file including contents is created under a folder.

var fileContent = 'sample text'; // As a sample, upload a text file.
var file = new Blob([fileContent], {type: 'text/plain'});
var metadata = {
    'name': 'sampleName', // Filename at Google Drive
    'mimeType': 'text/plain', // mimeType at Google Drive
    'parents': ['### folder ID ###'], // Folder ID at Google Drive
};

var accessToken = gapi.auth.getToken().access_token; // Here gapi is used for retrieving the access token.
var form = new FormData();
form.append('metadata', new Blob([JSON.stringify(metadata)], {type: 'application/json'}));
form.append('file', file);

var xhr = new XMLHttpRequest();
xhr.open('post', 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id');
xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
xhr.responseType = 'json';
xhr.onload = () => {
    console.log(xhr.response.id); // Retrieve uploaded file ID.
};
xhr.send(form);

請求正文:

在這個腳本中,form如下.使用 Drive API 的 create 方法將其發送到 Google Drive.

Request body :

In this script, form is as follows. This is sent to Google Drive using the create method of Drive API.

------WebKitFormBoundaryxX0XmxgooMjdUECR
Content-Disposition: form-data; name="metadata"; filename="blob"
Content-Type: application/json

{"name":"sampleName","mimeType":"text/plain","parents":["#####"]}
------WebKitFormBoundaryxX0XmxgooMjdUECR
Content-Disposition: form-data; name="file"; filename="blob"
Content-Type: text/plain

sample text
------WebKitFormBoundaryxX0XmxgooMjdUECR--

在我的環境中,我確認這可以正常工作.但如果這在您的環境中不起作用,我很抱歉.

In my environment, I confirmed that this works fine. But if this didn't work in your environment, I'm sorry.

這篇關于Google Drive API V3 Javascript - 創建包含內容的文件的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

discord.js v12: How do I await for messages in a DM channel?(discord.js v12:我如何等待 DM 頻道中的消息?)
how to make my bot mention the person who gave that bot command(如何讓我的機器人提及發出該機器人命令的人)
How to fix Must use import to load ES Module discord.js(如何修復必須使用導入來加載 ES 模塊 discord.js)
How to list all members from a specific server?(如何列出來自特定服務器的所有成員?)
Discord bot: Fix ‘FFMPEG not found’(Discord bot:修復“找不到 FFMPEG)
Welcome message when joining discord Server using discord.js(使用 discord.js 加入 discord 服務器時的歡迎消息)
主站蜘蛛池模板: 国产丝袜一区二区三区免费视频 | 一区二区视频在线 | www.亚洲 | 国产成人短视频在线观看 | 午夜a v电影 | 国产精品乱码一区二区三区 | 男人天堂午夜 | 欧美日韩国产精品一区二区 | 天天操操| 亚洲欧美一区二区三区1000 | 一本色道久久综合亚洲精品高清 | 免费久久网站 | 干干干日日日 | 国产精品成人69xxx免费视频 | 超碰男人天堂 | 欧美在线一区二区三区 | 国产精品精品久久久 | 成人在线免费观看 | 亚洲免费观看视频网站 | 啪啪精品 | 日本一二三区电影 | 一色桃子av一区二区 | 99久久婷婷国产综合精品电影 | 1级黄色大片 | 国产成人网 | 91视视频在线观看入口直接观看 | 久久手机视频 | 中文字幕在线三区 | 国产精品s色 | 国产激情视频在线免费观看 | 久久aⅴ乱码一区二区三区 91综合网 | 91精品国产综合久久久亚洲 | 国产美女在线播放 | 久久久精品一区 | 农村黄性色生活片 | 欧美aⅴ片 | 狠狠操狠狠操 | 99久热在线精品视频观看 | 欧美亚洲国产精品 | 精品国产一区二区三区在线观看 | 国产一区二区 |