Uploading image to google drive using enyo.ajax post request

edited July 2013 in Newbie Questions
I am trying to achieve this
POST /upload/drive/v2/files?uploadType=media HTTP/1.1
Host: www.googleapis.com
Content-Type: image/jpeg
Content-Length: {number_of_bytes_in_JPEG_file}
Authorization: {your_auth_token}

{JPEG data}
which is nothing but a simple file upload using google drive API. I am trying to upload an image to google drive. I use this as the reference
https://developers.google.com/drive/manage-uploads

I am using enyo with phonegap and here is my code.
var reader = new FileReader();
var fileContent = this.$.fileInput.getFiles()[0];
reader.readAsBinaryString(fileContent);
reader.onload = function(e) {
                  var contentType = fileContent.type;
                  var contentSize = fileContent.size;
                  var authCode = access_token;
                  var requestBody = reader.result;
                  var driveAjax = new enyo.Ajax({
                                                url: 'https://www.googleapis.com/upload/drive/v2/files?uploadType=media',
                                                method: 'POST',
                                                headers: {
                                                   'Content-Type' : contentType, 
                                                   'Content-Length' : contentSize,
                                                   'Authorization' : authCode
                                          },
                                                postBody: requestBody
                                          });
                  driveAjax.response(this, "uploadSuccess");
                  driveAjax.error(this, "processError");
                  driveAjax.go();
}
I have checked all the data and that is correct. But I get neither success nor any error. Can anyone tell where am I going wrong.

Comments

  • Are you able to do this operation from your computer using curl? That's a good starting point, as it can point to server rejection methods.
  • I just want to jump in here for a moment.

    I believe the reason you're seeing what you are is due to variable scoping. You see, you're calling this from inside a function that's not scoped to the same this as your code outside of that function. To fix that I'd recommend you do the following:
    reader.onload = enyo.bind(this, function(fileContent, e) {
                      (your code here)
    }, fileContent);
    What that does is ensure that you have the proper scope to the proper this, but also provides your function with the fileContent variable.
  • Hey Arthur ..... Thanks for your help. I at least got a 401 response. Now I can work on that. Thanks
Sign In or Register to comment.