Google Drive Rest API [英] Google Drive Rest API
问题描述
我遇到了Google Drive Rest API的问题.我有一个按钮,当用户单击时,我从后端获取了一个Blob Excel文件,并将该文件上传到Google驱动器.该文件正在上传到google驱动器,但是当我打开它时,它显示为"[object blob]".实际内容不在文件中.这是我创建文件的功能.我从这里找到了这个解决方案:使用Google Drive Api v3(javascript)创建文件
I am running into a problem with google drive rest api. I have a button and upon the user click, I get a blob excel file from my backend and upload the file to google drive. The file is being uploaded to the google drive, but when I opened it, it says '[object blob]'. The actual content isn't in the file. Here is my function for creating the file. I found this solution from here: Create File with Google Drive Api v3 (javascript)
var UploadExcelFile = function(name, data, callback){
const boundary = '-------314159265358979323846';
const delimiter = "\r\n--" + boundary + "\r\n";
const close_delim = "\r\n--" + boundary + "--";
const contentType = "application/vnd.google-apps.spreadsheet";
var metadata = {
'name': name,
'mimeType': contentType
};
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: ' + contentType + '\r\n\r\n' +
data +
close_delim;
var request = gapi.client.request({
'path': '/upload/drive/v3/files',
'method': 'POST',
'params': {'uploadType': 'multipart'},
'headers': {
'Content-Type': 'multipart/related; boundary="' + boundary + '"'
},
'body': multipartRequestBody});
if (!callback) {
callback = function(file) {
console.log(file)
};
}
request.execute(callback);
}```
```This is the response from the server:
Response {type: "basic", url:
"http://localhost:54878/home/generateexcel", redirected: false,
status:
200, ok: true, …}
body: ReadableStream
locked: true
__proto__: ReadableStream
bodyUsed: true
headers: Headers
__proto__: Headers
ok: true
redirected: false
status: 200
statusText: "OK"
type: "basic"
url: "http://localhost:54878/home/generateexcel"}
我正在从后端传递文件名和Blob excel文件的名称,如下所示:
I am passing in the name of the file and the blob excel file from the backend like so:
fetch('/home/generateexcel', {
method: 'POST',
body: JSON.stringify(postData),
headers: {
"Content-Type": "application/json"
},
}).then(function (response) {
response.blob().then(function (result)
UploadExcelFile('newfile', result)
});
}).catch(function (err) {
// Error :(
});
推荐答案
- 您要将下载的xlsx文件上传到Google云端硬盘.
- 您已经确认可以下载xlsx文件.
- 上传xlsx文件后,您想要转换为Google Spreadsheet.
- 您可以使用Drive API和访问令牌来上传文件.
如果我的理解是正确的,那么该修改如何?在此修改中,我使用FormData()
来创建请求正文,并使用fetch()
来请求Drive API.我认为针对您的情况有几种解决方案.因此,请仅考虑其中之一.
If my understanding is correct, how about this modification? In this modification, I used FormData()
for creating the request body and used fetch()
for requesting to Drive API. I think that there are several solutions for your situation. So please think of this as just one of them.
我修改了UploadExcelFile()
.请进行如下修改,然后重试.
I modified UploadExcelFile()
. Please modify as follows and try again.
function UploadExcelFile(name, data) {
var metadata = {
name: name,
mimeType: "application/vnd.google-apps.spreadsheet",
};
var form = new FormData();
form.append('metadata', new Blob([JSON.stringify(metadata)], {type: 'application/json'}));
form.append('file', data);
fetch('https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id,name,kind', {
method: 'POST',
headers: new Headers({'Authorization': 'Bearer ' + gapi.auth.getToken().access_token}),
body: form
}).then((res) => {
return res.json();
}).then(function(val) {
console.log(val);
});
}
在我的环境中,我可以确认此脚本有效.但是,如果这在您的环境中不起作用,我深表歉意.
In my environment, I could confirm that this script worked. But if this didn't work in your environment, I apologize.
这篇关于Google Drive Rest API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!