如何使用 Dropzone.js 按文件类型更改上传目标?

How do you change the upload destination by file type using Dropzone.js?

我需要将不同类型的文件发送到不同的目的地。

您如何重写 Dropzone.prototype.processQueueDropzone.prototype.processFiles 以发送到不同的目的地。

我不确定 if/what 异步解决方法是否需要到位才能将 this.options.url 换入和换出。

我相信您可以通过使用 'Dropzone.options' 对象来实现这一点。 编辑:这是文档 http://www.dropzonejs.com/#configuration

// "myAwesomeDropzone" is the camelized version of the HTML element's ID
Dropzone.options.myAwesomeDropzone = {
  paramName: "file", // The name that will be used to transfer the file
  maxFilesize: 2, // MB
  accept: function(file, done) {
    // if file name includes .jpg set the upload url to the correct jpg folder
    if (file.name.includes('.jpg')) {
      url: 'path/to/jpg/files';
      done();
    }
    // same as above but for .png files, you could also use a switch statement
    if (file.name.includes('.png')) {
      url: 'path/to/png/files';
      done();
    }
  }
};

虽然@Inkdot 的回答在技术上是正确的,但这并不是最好的方法,因为这可能会导致竞争条件。

最好的方法是使用一个函数作为url参数:

Dropzone.options.url = function(files) {
  let url = 'upload/path';
  if (/(jpg|jpeg)$/.test(files[0])) url = 'path/to/jpeg';
  return url;
}

这个函数会在uploadFiles()函数中调用,也就是真正上传文件的步骤。

有关详细信息,请参阅 the documentation on the url parameter