There are two problems:
- The JavaScript client library does not support multimedia downloads.
- Google Docs files do not have their own format.
You can get around problem # 1 by writing your own boot functions built on top of XHR. The following code should work on most modern web browsers:
function updateFileContent(fileId, contentBlob, callback) { var xhr = new XMLHttpRequest(); xhr.responseType = 'json'; xhr.onreadystatechange = function() { if (xhr.readyState != XMLHttpRequest.DONE) { return; } callback(xhr.response); }; xhr.open('PATCH', 'https://www.googleapis.com/upload/drive/v3/files/' + fileId + '?uploadType=media'); xhr.setRequestHeader('Authorization', 'Bearer ' + gapi.auth.getToken().access_token); xhr.send(contentBlob); }
To get around problem # 2, you can send to the disk a file type that Google documents can import, such as .txt, .docx, etc. The following code uses the function above to update Google Doc content using plain text
function run() { var docId = '...'; var content = 'Hello World'; var contentBlob = new Blob([content], { 'type': 'text/plain' }); updateFileContent(fileId, contentBlob, function(response) { console.log(response); }); }
Eric Koleda
source share