Search code examples
google-docs-apigoogle-drive-api

Google docs file upload and move collection issue


Issue #1

When i'm uploading a file to google docs i receive status code "201" created, but when i try to open the file it seems that i'm doing something wrong, because i can't open it, and when i'm trying to download and open it on my PC i see the binary data instead of text or image. Current language is APEX, but i think it's pretty understandable.

First of all i'm getting Upload URL and then putting data to this URL;

public void getUploadURL()
{
    Httprequest req = new Httprequest();
    req.setEndpoint('https://docs.google.com/feeds/upload/create-session/default/private/full?convert=false');
    req.setMethod('POST');
    req.setHeader('GData-Version', '3.0');
    req.setHeader('Authorization', 'OAuth '+accessToken);
    req.setHeader('Content-Length', '359');
    req.setHeader('X-Upload-Content-Type', fileType);
    req.setHeader('X-Upload-Content-Length', fileSize);


    Dom.Document requestDoc = new Dom.Document();

    String xml =
            '<?xml version=\'1.0\' encoding=\'UTF-8\'?>'
            +'<entry xmlns="http://www.w3.org/2005/Atom" xmlns:docs="http://schemas.google.com/docs/2007">'
            +'<title>'+fileName+'</title></entry>';

    requestDoc.load(xml);
    req.setBodyDocument(requestDoc);
    Http h = new Http();
    Httpresponse res = h.send(req);
    System.debug('response=\n'+res.getHeader('Location'));
    uploadFIle(res.getHeader('Location'));
}

public void uploadFIle(String uploadUrl)
{
    Httprequest req = new Httprequest();
    req.setEndpoint(uploadUrl);
    req.setMethod('PUT');
    req.setHeader('GData-Version', '3.0');
    req.setHeader('Authorization', 'OAuth '+accessToken);
    req.setHeader('Host', 'docs.google.com'); 
    req.setHeader('Content-Length', fileSize);
    req.setHeader('Content-Type', fileType);
    req.setBody(''+binaryData);
    Http h = new Http();
    Httpresponse res = h.send(req);
    System.debug('response=\n'+res.getBody());
}

As for "binaryData" property - i receive it from the page using javascript like this:

<input type="file" id="myuploadfield" onchange="getBinary()"/>
    <script>
        function getBinary()
        {
            var file = document.getElementById('myuploadfield').files[0];
            fileSizeToController.val(file.size.toString());
            fileNameToController.val(file.name.toString());
            fileTypeToController.val(file.type.toString());
            var r = new FileReader();
            r.onload = function(){ binaryToController.val(r.result); };
            r.readAsBinaryString(file);
        }
    </script>

r.onload = function(){ binaryToController.val(r.result); }; - this is the string that sends file binary data to my controller.

Issue #2

I'm trying to move one collection(folder) to another, and using this article (protocol tab instead of .NET). The issue is that i need to move collection instead of copying it and when i add my collection to another using this article, i'm currently adding reference to my collection instead of moving the whole collection from one place to another.

Please tell me what am i doing wrong.

Thank you for consideration.


Solution

  • Your "binary" data is being corrupted, when you are performing '' + binaryData.

    In general, I have had more success using slicing of files, here is an example for webkit:

    var chunk = this.file.webkitSlice(startByte, startByte + chunkSize, file_type);
    // Upload the chunk
    uploadChunk(startByte, chunk, callback);