首页 > 解决方案 > google drive api Resumable upload error 400 Parse Error?

问题描述

“分段上传”是正常的,但“可恢复上传”不是。

我的“代码”有什么问题?

用户网络中断时api是否需要重传文件?

文件是否会从中断中重新传输?

对不起,我的英语很差

function uploadPostBlob(selectedFile) {

  //selectFile is File Object
  var metadata = {
    'name': selectedFile.name, // Filename at Google Drive
    'mimeType': selectedFile.mimeType, // mimeType at Google Drive
    'parents': ['root'], // Folder ID at Google Drive
  };
  var accessToken = gapi.auth.getToken().access_token; // Here gapi is used for retrieving the access token.
  var form = new FormData();
  form.append('metadata', new Blob([JSON.stringify(metadata)], {
    type: 'application/json'
  }));
  form.append('file', selectedFile);

  //https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable
  //https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart

  fetch('https://www.googleapis.com/upload/drive/v3/files?uploadType=resumable', {
    method: 'POST',
    headers: new Headers({
      'Authorization': 'Bearer ' + accessToken,
      'Content-Type':'application/json; charset=UTF-8'

    }),
    body: form,
  }).then((res) => {

    return res.json();

  }).then(function(val) {
    console.log(val);
  });
}

标签: javascriptajaxgoogle-drive-apifetch

解决方案


推荐阅读