如何创建zip文件并添加文件并以角度6发送到服务器

时间:2019-06-07 11:21:43

标签: angular angular6 zipfile jszip nodejs-jszip

  • 大家好
  • 使用angular6并创建邮政编码JSZIP
  • 正在上传多个输入文件

html代码:

<input type="file" id="selectedFile" (change)="upload($event)" multiple />

角度ts文件

upload(event) {
console.log('input files list : ', event.target.files); // files list showing crrectly
this._createZipFile(event.target.files)
}

private _createZipFile(inputFiles) {
    const _zip = new JSZip();
    let count = 0;
    const zipFilename = 'zipFilename.zip';

    // inputFiles.prototype.forEach(function (file) {
    const filename = 'filename';

    // loading a file and add it in a zip file
    JSZipUtils.getBinaryContent(inputFiles[0].file, function (err, data) {
      if (err) {
        throw err; // or handle the error
      }
      _zip.file(filename, data, { binary: true });
      count++;
      if (count === inputFiles.length) {
        /* const zipFile = JSZip.generateAsync({ type: 'blob' });
        // saveAs(zipFile, zipFilename);
        console.log('what is zip file??? ', zipFile); */
        if (count === inputFiles.length) {
          _zip.generateAsync({type: 'blob'}).then(function(content) {
            // saveAs(content, zipFilename);
            // i need zip file to send to server
            const file: File = newZipFile; // Here need created zip file
            const formData: FormData = new FormData();
            formData.append('uploadFile', file, file.name);
         });
        }
      }
    });
    // });
  }
  • 但无法获取zip文件,我做错了,得到了更多错误。.
  • 请为此提供帮助,并节省我的时间,在此先感谢...

0 个答案:

没有答案