Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload html document to Google drive and make it editable as Google document

The app I'm working on generates a simple (less than 100K) html documents that does not include any links or images.

I want to "upload" this document directly from the browser to Google drive so that a user can subsequently edit the document on Google drive, taking advantage of its multi-user sharing and editing capabilities.

I can successfully upload the html document to Google drive with this code:

function saveToDrive(tx){
  var boundary = '-------314159265358979323846';
  var delimiter = "\r\n--" + boundary + "\r\n";
  var close_delim = "\r\n--" + boundary + "--";
  var doc = "<html><body>" + tx.html + "</body></html>";
  var metadata = {
     'title': tx.client_name,
     'mimeType': 'application/json'
  };

  var multipartRequestBody =
     delimiter +
        'Content-Type: application/json\r\n\r\n' +
        JSON.stringify(metadata) +
        delimiter +
        'Content-Type: ' + 'text/html' + '\r\n' +
        '\r\n' +
        doc +
        close_delim;

  gapi.client.request({
     'path': '/upload/drive/v2/files',
     'method': 'POST',
     'params': {'uploadType': 'multipart'},
     'headers': {
        'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
     },
     'body': multipartRequestBody
  }).execute(function(res){console.log(res)});

}

This uploads results in a document on Google drive that cannot be edited or opened as a Google document.

I have also tried using 'application/vnd.google-apps.document' as the mimeType of the document.

Is there a way to convert the document to a format that can be edited on drive as a Google document? Note I have tried added convert=true as a parameter but that didn't work.

like image 561
rob_hicks Avatar asked Jan 01 '26 00:01

rob_hicks


1 Answers

Are you sure convert:true parameter did not work? I got your code working for me. Everything is the same as you have above, except 'convert':'true' and Authorization header was added.

 var request = gapi.client.request({
 'path': '/upload/drive/v2/files',
 'method': 'POST',
 'params': {'uploadType': 'multipart', 'convert':'true'},
 'headers': {
     'Authorization':'Bearer '+tokenGot,
    'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
 },
 'body': multipartRequestBody
like image 147
Barry Owens Avatar answered Jan 04 '26 12:01

Barry Owens



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!