如何更改url dropzone?具有ajax成功的动态URL

时间:2015-05-11 04:46:32

标签: dropzone.js

我读到这个:https://github.com/enyo/dropzone/wiki/Set-URL-dynamically但我没有成功...... :( 我有1个表格......

我用ajax发送输入。

ajax返回用户的新id。在这一刻我想改变de url dropzone以设置新用户的id路径。

$.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,

});
抱歉我的英语不好!

谢谢大家。

5 个答案:

答案 0 :(得分:18)

您可以在dropzone的“处理”事件监听器上添加一个函数。

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

以下是我获取代码的链接,它适用于我:https://github.com/enyo/dropzone/wiki/Set-URL-dynamically

答案 1 :(得分:3)

更改此

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

到这个

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

这有用吗?

答案 2 :(得分:1)

如果您需要为每个文件动态更改URL dropzone帖子,可以使用processingfile事件并更改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>

答案 3 :(得分:1)

旧问题的新答案只是因为我找到了这个答案以及dropzone wiki的链接并且不喜欢它。多次修改插件的选项似乎非常错误。

当dropzone使用某些选项时,它会通过传入文件数组的resolveOption函数运行它。在当前分支中,您可以为选项定义一个函数:method,url和timeout。

这是一个完整的工作示例,包括延迟ajax:

&#13;
&#13;
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
});
&#13;
<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>
&#13;
&#13;
&#13;

答案 4 :(得分:0)

对我有用的另一种方法(接受事件回调):

$('div#dropzone').dropzone({
    options...,
    accept: function (file, done) {
        this.options.url = 'the url you want';
    }
});
相关问题