Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Dropzone dynamically change POST location

I have an issue regarding Dropzone.js, I am looking to be able to change the post URL dynamically such as :

window.onload = function() {
  myDropzone = new Dropzone("#my-awesome-dropzone", {
    url: "upload_file.php"
  });
  myDropzone.on("complete", function(file) {
    myDropzone.removeFile(file);
  });
}

Where you see url: "upload_file.php", I would like to change this property as and when I need to, this to enable me to change the path that files are stored so i can do something similar to:

url: "upload_file.php?path=/path/to/folder/"
like image 526
P Clegg Avatar asked Dec 19 '22 08:12

P Clegg


1 Answers

The provided answers are incomplete without combining them. The one from the OP is the recommended way provided by the creator of dropzone.

Dropzone.options.myDropzone = {
  init: function() {
    this.on("processing", function(file) { // was processingfile
      this.options.url = "/some-other-url";
    });
  }
};

This can be seen in the Wiki and #94.

However, a more dynamic possibility is to use Dropzone.options.myDropzone.options.url. Obviously this is very extensive and can be pulled in a few different ways.

One being:

// assuming you have autodiscover off or you are using the whole body as a dropzone
Dropzone.options.myDropzone = new Dropzone(document.body, {
    url: '/default/url',
    ...
});

var myDropzone = Dropzone.options.myDropzone, url = "Your url";

function doSomething(dropzone, url){
    dropzone.options.url = url
}

doSomething(myDropzone, url)
console.log(myDrozone.options.url);

I currently prefer the second approach, but the first is obviously useful in its own ways.

like image 165
Cayce K Avatar answered Apr 28 '23 04:04

Cayce K