Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change url dropzone? URL dynamically with ajax success

Tags:

dropzone.js

I read this: https://github.com/enyo/dropzone/wiki/Set-URL-dynamically but i dont got success... :( I have 1 form...

And i send the inputs with ajax.

The ajax returns the new id of user. in this moment i want to change de url dropzone for to set path to id of the new user.

$.ajax({
    type: "POST",
    url: "class/inserir.php?funcao=teste",
    data: formdata,
    dataType: "json",
        success: function(json){
        if(json.sucesso=="sim"){
            alert("Wait! Sending Pictures.");
                    this.options.url = "class/upload_img.php?"+json.id;
            myDropzone.processQueue(); 
        }else{
        location.href="home.php?ir=cad_animal&cad=nao&erro="+json.erro;
        }
    }
});

var myDropzone = new Dropzone("#imagens", {

    url: "class/upload_imgteste.php",
    paramName: "file", // The name that will be used to transfer the file
    maxFilesize: 1, // MB
    addRemoveLinks : true,
    dictResponseError: "Não foi possível enviar o arquivo!",
    autoProcessQueue: false,
    thumbnailWidth: 138,
    thumbnailHeight: 120,

});

sorry for my bad english!

Thanks for all.

like image 240
Marcelo Pessoa Avatar asked May 11 '15 04:05

Marcelo Pessoa


4 Answers

change this

this.options.url = "class/upload_img.php?"+json.id;

to this

myDropzone.options.url = "class/upload_img.php?"+json.id;

Does that work?

like image 175
Torqie Avatar answered Oct 23 '22 06:10

Torqie


You may add a function on dropzone's "processing" event listener.

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

Here is the link where I got the code and it works for me: https://github.com/enyo/dropzone/wiki/Set-URL-dynamically

like image 39
jroi_web Avatar answered Oct 23 '22 06:10

jroi_web


New answer for an old question only because I found this answer and the link to the dropzone wiki and didn't like it. Modifying the options of the plugin multiple times like that seems very wrong.

When dropzone uses some options it runs it through a resolveOption function passing in a files array. In the current branch you can define a function for the options: method, url and timeout.

Here's a complete working example including delaying for the ajax:

Dropzone.autoDiscover = false;

const doStuffAsync = (file, done) => {
  fetch('https://httpbin.org/get').then((response) => {
    file.dynamicUploadUrl = `https://This-URL-will-be-different-for-every-file${Math.random()}`
    done();//call the dropzone done
  })  
}

const getMeSomeUrl = (files) => {
  return `${files[0].dynamicUploadUrl}?sugar&spice`;
}

let myDropzone = new Dropzone("#my-awesome-dropzone", {
  method: "put",
  accept: doStuffAsync,
  url: getMeSomeUrl
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.4.0/min/dropzone.min.js"></script>
<link rel="stylesheet" type="text/css" href="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.4.0/min/dropzone.min.css">

<form action="/file-upload" class="dropzone" id="my-awesome-dropzone">
</form>
like image 5
BlueWater86 Avatar answered Oct 23 '22 07:10

BlueWater86


If you need to change the URL dropzone posts to dynamically for each file, you can use the processingfile event and change the options.url.

<form id="my-dropzone" action="/some-url" class="dropzone"></form>
<script>
Dropzone.options.myDropzone = {
  init: function() {
    this.on("processing", function(file) {
      this.options.url = "/some-other-url";
    });
  }
};
</script>
like image 1
Saleh Mosleh Avatar answered Oct 23 '22 06:10

Saleh Mosleh