1

我正在使用 XMLHttpRequest 上传文件。在事件完成时,我想要文件名 - 服务器(AWS S3)不返回包含该信息的响应。访问此文件名的正确方法是什么?如果我用 xhr.filename = 'abc.jpg' 重载 XMLHttpRequest,我可以在事件中访问它,但似乎有点不干净。我尝试设置请求标头,但在返回的事件中看不到访问该标头的方法。有没有更好的方法(是的,我可以重新格式化我的范围等,但我对访问返回事件中的文件名特别感兴趣。

function upload() {

  var xhr = new XMLHttpRequest();
  xhr.upload.addEventListener("progress", uploadProgress, false);
  xhr.addEventListener("load", uploadComplete, false);
  xhr.addEventListener("error", uploadFailed, false);
  xhr.addEventListener("abort", uploadCanceled, false);

  xhr.setRequestHeader("X-File-Name", file.name);  // how to access this?
  xhr.filename = file.name; // works but seems a bit unclean imo

  scope.progressVisible = true;
  xhr.send(data);


  }, function(err) {
    console.log(err);
  });
};


function uploadComplete(evt) {
   // I want the file name here as part of this function's parameters (i.e. evt - doesn't seem to return anymore params than that)
}
4

0 回答 0