我一直在研究 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。你能告诉我为什么吗?我不能将文件下载到我的磁盘..?
这是使用媒体作为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;
}
}