无法上传启用宏的文件,例如.docm / .pptm / .xlsm

时间:2017-09-13 18:09:00

标签: javascript google-drive-api drive

我尝试编写简单的JavaScript代码,使用Drive REST api将文件转换并上传到Google云端硬盘(这样只会上传转换后的文档到Google文档格式)。我在上传docx / xlsx / pptx文件方面犹豫不决,但是当我尝试启用宏的文件,例如.docm / .pptm / .xlsm 时,我收到错误“错误请求”,如屏幕截图所示:{ {3}}

我的JavaScript代码如下:

<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<input type="file" id="fileToUpload"></input>
<button onclick="insertFile()">Pick File to Upload</button>
<script type="text/javascript">
var CLIENT_ID = 'CLIENT_ID_HERE';
var SCOPES = 'https://www.googleapis.com/auth/drive';
var FOLDER_ID = '';

/**
 * Called when the client library is loaded to start the auth flow.
 */
function handleClientLoad() {
  window.setTimeout(checkAuth, 1);
}

/**
 * Check if the current user has authorized the application.
 */
function checkAuth() {
  gapi.auth.authorize(
      {'client_id': CLIENT_ID, 'scope': SCOPES},
      handleAuthResult);
}

/**
 * Called when authorization server replies.
 *
 * @param {Object} authResult Authorization result.
 */
function handleAuthResult(authResult) {
  if (authResult && !authResult.error) {
      console.log("Auth OK !!!");
  }
}

function insertFile() {
  fileData = document.getElementById("fileToUpload").files[0];
  const boundary = '-------314159265358979323846';
  const delimiter = "\r\n--" + boundary + "\r\n";
  const close_delim = "\r\n--" + boundary + "--";
  var reader = new FileReader();
  reader.readAsBinaryString(fileData);
  reader.onload = function(e) {
    var contentType = fileData.type || 'application/octet-stream';
    var metadata = {
      'title': fileData.fileName,
      'mimeType': contentType
    };

    var base64Data = btoa(reader.result);
    var multipartRequestBody =
        delimiter +
        'Content-Type: application/json\r\n\r\n' +
        JSON.stringify(metadata) +
        delimiter +
        'Content-Type: ' + contentType + '\r\n' +
        'Content-Transfer-Encoding: base64\r\n' +
        '\r\n' +
        base64Data +
        close_delim;

    var request = gapi.client.request({
        'path': '/upload/drive/v2/files',
        'method': 'POST',
        'params': {'convert': true},
        'headers': {
          'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
        },
        'body': multipartRequestBody});

    request.execute((file) => {
      console.log(file);
    });
  }
}
</script>
<script type="text/javascript" src="https://apis.google.com/js/client.js?onload=handleClientLoad"></script>

编辑: 之前我认为'Google Drive不支持转换启用宏的文件'。但是,当我尝试使用python脚本转换并上传启用宏的文件时,我能够做到这一点。 Python代码:

http = httplib2.Http()
credentials.authorize(http)
drive_service = apiclient.discovery.build('drive', 'v2', http=http)

metadata = {'title': 'test_file.docm',
'mimeType' : 'application/vnd.ms-word.document.macroEnabled.12',}

res = drive_service.files().insert(convert='True',body=metadata,
media_body='test_file.docm', fields='mimeType,exportLinks').execute()

我在Javascript代码中遗漏了什么吗?

0 个答案:

没有答案