如何下载和上传 zip 文件而不保存到磁盘

can*_*man 1 javascript jquery reactjs

 $.ajax({
      url: 'url.com/myfile.zip',
    })
      .then((data) => {
        const blob = new Blob([parsed_data], {type: 'application/octet-stream'});
        const file = new File([blob], filename, {type: 'application/zip'});
        this.handleUpload(file); // Sends POST request with received file
      });   
Run Code Online (Sandbox Code Playgroud)

我正在尝试下载并立即上传一个 zip 文件。然而,上传端点不会将接收到的文件识别为 zip,尽管它是作为 zip 下载的,但被视为类型字符串。我需要一种方法来处理我的承诺中的文件而无需解压缩。有任何想法吗?

art*_*tgb 6

您可以像这样以二进制格式获取数据。

xhr.open('GET', 'url.com/myfile.zip', true);
xhr.responseType = 'blob';

xhr.onload = function(e) {
  if (this.status == 200) {
    var data = this.response;
    const blob = new Blob(data, {type: 'application/octet-stream'});
    const file = new File(blob, filename, {type: 'application/zip'});
    this.handleUpload(file); // Sends POST request with received file
  }
};

xhr.send();
Run Code Online (Sandbox Code Playgroud)