在 Dropzone 上传之前压缩文件

Tah*_*bal 5 javascript zip dropzone.js

有没有办法从放置在 dropzone 中的文件生成 zip,然后将该 zip 文件发送到服务器?

我是从文件中使用生成ZIPJSZipsending悬浮窗的事件:

this.on("sending", function(file, xhr, formData) {
  var zip = new JSZip();
  zip.file("Hello.csv", file);
  zip.generateAsync({ type: "blob" }).then(function(content) {
    // see FileSaver.js
    saveAs(content, "example.zip");
  });
});
Run Code Online (Sandbox Code Playgroud)

如何让 dropzone 发送此文件而不是添加的一个用户?

nim*_*imi 1

这在 dropzone 5.7.0 上对我有用,但我使用了该"addedFile"事件:

this.on("addedfile", function (file) {
  if (file.done) {
    return;
  }
  const dz = this;
  dz.removeFile(file);
  let z = new JSZip();
  z.file(file.name, file);
  z.generateAsync({ 
    type: "blob",
    compression: "DEFLATE",
    compressionOptions: { level: 9}
    }).then(function(content) {    
      let f = new File([content], file.name);
      f.done = true;
      dz.addFile(f);
  });
});
Run Code Online (Sandbox Code Playgroud)

我添加了压缩(JSZip 的默认值是STORE)并保留了file.name以使压缩对用户透明(但是他们仍然会看到较小的尺寸)。

ps 我也不喜欢添加该f.done字段...欢迎更好的解决方案。