我一直致力于G驱动API,并使用JavaScript for Windows 8上传,下载文件和文件夹的删除和更新。我遇到的两个问题是;
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); });
以下是使用media为uploadtype插入文件的完整功能。
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;
}
}
答案 0 :(得分:2)
如果您要使用该文件上传元数据,则需要实施multipart
上传。它是在https://developers.google.com/drive/manage-uploads#multipart