使用Google Drive API javascript上传或更新文件时标题为“无标题”?

时间:2013-05-24 17:40:34

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

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

  1. 当我上传文件或文件夹时,会上传标题为“UNTITLED”,你能告诉我我做错了吗?我的要求是这样的;
  2. 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。你能告诉我为什么吗?我不能把文件下载到我的磁盘..?
    2. 以下是使用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;
             }
      
      
         }
      

1 个答案:

答案 0 :(得分:2)

如果您要使用该文件上传元数据,则需要实施multipart上传。它是在https://developers.google.com/drive/manage-uploads#multipart

上解释的