2

I have been working on G drive API and doing upload, download delete and update of files and folders using JavaScript for windows 8 . The two problems which I encountered are;

  1. When I upload a file or folder it gets uploaded with a title "UNTITLED" , can you tell me what I am doing wrong.? my request looks like this;
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. When I download a file I get an encrypted or some unique type of response like if I download an JPEG image I get JFIF in response text. Can you please tell me why ? cant I get the file downloaded to my disk..?

Here is the complete function of insertion of file using media as 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 Answers1

2

If you would like to upload metadata with the file, you need to implement the multipart upload. It's explained on https://developers.google.com/drive/manage-uploads#multipart

Burcu Dogan
  • 9,153
  • 4
  • 34
  • 34
  • I have used the multipart but it only solves one half of the problem, the title gets set, but file can't be previewed, but when I use the media as the upload type then file is previewed but title is not set. Above is the complete insert function using MEDIA as upload type. – user2418457 May 26 '13 at 08:23