windows-8 - 使用 Google Drive API javascript 上传或更新文件时标题为 'Untitled'?

标签 windows-8 google-drive-api winjs google-drive-realtime-api

我一直在研究 G 驱动器 API,并使用 Windows 8 的 JavaScript 上传、下载、删除和更新文件和文件夹。我遇到的两个问题是;

  1. 当我上传文件或文件夹时,它会以标题“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);

   });
  1. 当我下载文件时,我会得到加密的或某种独特类型的响应,就像我下载 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/

相关文章:

c# - winmd 文件的版本 255.255.255.255 表示什么?

windows-8 - 如何让 Windows 8 Phone 模拟器通过 VirtualBox 运行?

python - 使用服务帐户凭据的 GDrive 导出失败并显示 404

javascript - 全屏 WinJS.UI.FlipView?

windows-runtime - WinJS 窗口大小

javascript - 如何识别用户代理是否为 Windows 8 平板电脑?

windows - 在 Windows 上安装 npm 模块时出现 "cannot find module"

csv - Google Apps 脚本 txt 文件 getDataAsString() 返回字符之间的 � 和方形符号

c# - Google Drive V3 Api 获取文件名,C#

javascript - 解析数据馈送导致 WinJS 中出现无效字符错误