0

I have a need for files of different types to go to different destinations.

How might you rewrite Dropzone.prototype.processQueue or Dropzone.prototype.processFiles to send to different destinations.

I'm not sure if/what asynchronous work arounds need to be in place to swap this.options.url in and out.

Ashley Simons
  • 340
  • 4
  • 13

2 Answers2

1

Although @Inkdot's answer is technically correct, it is not the best way to do it, since this might result in a race condition.

The best way is to use a function as the url parameter:

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

This function will be invoked in the uploadFiles() function, which is the step that actually uploads the files.

See the documentation on the url parameter for more information.

enyo
  • 16,269
  • 9
  • 56
  • 73
  • There are no race conditions in Javascript. Inkdot's solution will work just fine. – Daniel T. Feb 20 '17 at 04:01
  • 1
    There are race conditions, because the `accept` function and the actual `upload` function might not be triggered in one thread, but asynchronously or not in the same order they have been added. I wrote the library, and the possibility to provide the `url` function for this exact reason. – enyo Feb 20 '17 at 14:33
  • Javascript only has one thread. Network requests are done natively by the browser and can run simultaneously, but the callbacks are done on the Javascript thread and can only be invoked sequentially. What you are describing is not a race condition, it's an AJAX callback ordering issue. You don't know the order that the callbacks will be called in, but that's not a race condition. – Daniel T. Feb 24 '17 at 04:01
  • 1
    Look up the definition of race condition. – enyo Mar 02 '17 at 15:08
  • I did, did you? This is a good read for you: https://blog.raananweber.com/2015/06/17/no-there-are-no-race-conditions-in-javascript/ – Daniel T. Mar 02 '17 at 23:07
  • The definition: "[...] the behavior [...] where the output is dependent on the sequence or timing of other uncontrollable events.". This is the reason Inkdot's solution "will *not* work just fine" because there might be an AJAX request in the middle, forcing the first upload into a different event loop (sorry, in my previous comment I wrote "thread" by mistake instead of "event loop", so that might have confused you). – enyo Mar 03 '17 at 21:49
  • What you mean then is that the value of `url` has changed between the `accept` function getting called and the `upload` function getting called. This is no different than if I did `x = 3; printX(); x = 4; printX();`. And I wish people would stop using the definition on Wikipedia, it's badly-written and can apply to a number of situations that are not race conditions. This short post might eludicate some things: https://davidcaylor.com/2011/04/15/async-doesnt-have-race-conditions/ – Daniel T. Mar 04 '17 at 00:55
  • Just because you don't like the definition of a race condition, it means something else for you or you would use it differently, doesn't mean it is invalid (this is not only wikipedias definition of a race condition, but pretty much every major dictionary or encyclopedia... race conditions have existed before multi-threaded programming languages, you know). Your comment was wrong in regards to the solution, and you are hung up on the meaning of race condition for no additional benefit to the actual question or answer. Your example is ridiculous which confirms that you're simply trolling. – enyo Mar 04 '17 at 11:46
  • *shrug* Stay ignorant if you want, but I'm not the only person who thinks you're wrong: https://stackoverflow.com/questions/21463377 "Many programming languages implements asynchronous programming features through events or signals, handled by a main loop or event loop which check for the event queue and trigger the listeners. Example of this are Javascript, libuevent, reactPHP, GNOME GLib... Sometimes, we can find situations which seems to be race conditions, but they are not." – Daniel T. Mar 05 '17 at 22:19
  • What don't you get about the fact, that race conditions are not exclusive to multi-threaded programming languages, and appear in many many different fields, like in logic circuits, file systems, networking, asynchronous programming, etc...? Or are you claiming that those are all not really race conditions, and that the only true race conditions apply in multi threaded languages...? – enyo Mar 08 '17 at 11:38
  • 1
    I've accepted this answer. It might be worth pointing out that `parallelUploads` should be set to 1. – Ashley Simons Mar 09 '17 at 07:30
  • @AshleySimons if you use the `url` config as a function, then using `parallelUploads` greater than 1 should work fine. Have you experienced issues with it? – enyo Mar 09 '17 at 11:14
  • @enyo At some point when I would drop multiple files some would go to the same destination as the first file. I also set auto process queue to true around the same time, but I believe the parallel setting did the trick. – Ashley Simons Mar 11 '17 at 11:39
  • And this also happens with the `url` function config? Because this is the exact same reason why I recommended this approach, because it should work fine. If so, it's a bug and I'll look into it. – enyo Mar 12 '17 at 13:04
0

I believe you can achieve this by using the 'Dropzone.options' object. edit: here's the documentation 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
  • 266
  • 1
  • 6