google drive api可恢复上传错误400解析错误?

时间:2019-08-01 07:33:03

标签: javascript ajax google-drive-api fetch

“分段上传”是正常的,但“断续上传”不是。

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

当用户网络中断时,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);
  });
}

0 个答案:

没有答案