P Clegg
P Clegg

Reputation: 697

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/"

Upvotes: 4

Views: 11645

Answers (3)

P Clegg
P Clegg

Reputation: 697

Sorted...

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

from: https://github.com/enyo/dropzone/wiki/Set-URL-dynamically

Upvotes: 5

Yiping
Yiping

Reputation: 1061

Just use

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

Upvotes: 2

Cayce K
Cayce K

Reputation: 2338

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.

Upvotes: 6

Related Questions