问题描述
之前有人问过这个问题,但答案是使用 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 - 创建包含内容的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!