【问题标题】:Upload html document to Google drive and make it editable as Google document将 html 文档上传到 Google 驱动器并使其可作为 Google 文档进行编辑
【发布时间】:2014-01-03 01:39:55
【问题描述】:

我正在开发的应用程序会生成一个简单(小于 100K)的 html 文档,其中不包含任何链接或图像。

我想将此文档直接从浏览器“上传”到 Google 云端硬盘,以便用户随后可以在 Google 云端硬盘上编辑该文档,从而利用其多用户共享和编辑功能。

我可以使用此代码成功地将 html 文档上传到 Google 驱动器:

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)});

}

这会将结果上传到 Google 驱动器上,该文档无法作为 Google 文档进行编辑或打开。

我还尝试使用“application/vnd.google-apps.document”作为文档的 mimeType。

有没有办法将文档转换为可以在驱动器上作为 Google 文档进行编辑的格式?请注意,我尝试添加 convert=true 作为参数,但没有奏效。

【问题讨论】:

    标签: google-drive-api


    【解决方案1】:

    您确定convert:true 参数不起作用吗?我让你的代码为我工作。除了添加了 'convert':'true'Authorization 标头之外,一切都与您上面的相同。

     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
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多