我一直在研究 G 驱动器 API,并使用 Windows 8 的 JavaScript 上传、下载、删除和更新文件和文件夹。我遇到的两个问题是;
- 当我上传文件或文件夹时,它会以标题“UNTITLED”上传,您能告诉我我做错了什么吗?我的请求是这样的;
WinJs.xhr ({ url: 'https://www.googleapis.com/upload/drive/v2/files?uploadType=media', type: "post", headers: { "Authorization": "Bearer " + accessToken, }, data: { "mimeType": "application/vnd.google-apps.folder", "title": "sampleFolder2" } }).then (function (success) { var response = JSON.parse(success.response); }, function (error) { console.log(error); });
- 当我下载文件时,我会得到加密的或某种独特类型的响应,就像我下载 JPEG 图像时,我会在响应文本中得到 JFIF。你能告诉我为什么吗?我不能将文件下载到我的磁盘上吗?
这里是使用媒体作为上传类型插入文件的完整功能。
function insertFile(accessToken) {
var openPicker = new Windows.Storage.Pickers.FileOpenPicker();
openPicker.viewMode = Windows.Storage.Pickers.PickerViewMode.thumbnail;
openPicker.fileTypeFilter.replaceAll([".png", ".jpg", ".jpeg", ".txt"]);
openPicker.pickSingleFileAsync().then(function (file) {
if (file) {
var tMeta = {
title: file.name,
mimeType: file.contentType,
description: "xyz description."
};
WinJS.xhr({
url: 'https://www.googleapis.com/upload/drive/v2/files?uploadType=media',
type: "post",
headers: {
"Authorization": "Bearer " + accessToken,
'Content-Type': file.contentType,
'Title': file.name
},
body: tMeta,
}).then(function (success) {
var response = JSON.parse(success.response);
var file1 = response;
secondRequest(accessToken, file1 , file);
}, function (error) {
var x = 4;
});
}
});
}
function secondRequest(accessToken, file1,file) {
var x = 2;
var tMeta = {
title: file.name,
mimeType: file1.mimeType,
// description: "xyz description."
};
var URL = 'https://www.googleapis.com/upload/drive/v2/files/' + file1.id + '?uploadType=media'
WinJS.xhr({
url: URL,
type: "put",
headers: {
"Authorization": "Bearer " + accessToken,
'Content-Type': file1.mimeType,
'Title': file.name
},
body: tMeta,
data: MSApp.createFileFromStorageFile(file)
}).then(function (success) {
var secondResponse = JSON.parse(success.response);
var z = 3;
}), function (error) {
var x = 3;
}
}
最佳答案
如果您想随文件一起上传元数据,则需要实现分段
上传。 https://developers.google.com/drive/manage-uploads#multipart 上对此进行了解释
关于windows-8 - 使用 Google Drive API javascript 上传或更新文件时标题为 'Untitled'?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16740710/