【问题标题】:How to change url dropzone? URL dynamically with ajax success如何更改 url dropzone? ajax成功的动态URL
【发布时间】:2015-07-21 11:52:41
【问题描述】:

我读到这个: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,

});

对不起我的英语不好!

谢谢大家。

【问题讨论】:

    标签: dropzone.js


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      旧问题的新答案只是因为我找到了这个答案和 dropzone wiki 的链接并且不喜欢它。像这样多次修改插件的选项似乎很不对。

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

      这是一个完整的工作示例,包括 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>

      【讨论】:

        【解决方案3】:

        如果您需要为每个文件动态更改 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>
        

        【讨论】:

          【解决方案4】:

          您可以在 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

          【讨论】:

          • 最佳答案
          • 正确答案。谢谢。
          【解决方案5】:

          改变这个

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

          到这里

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

          这行得通吗?

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-10-31
            • 1970-01-01
            • 2016-08-21
            • 1970-01-01
            • 1970-01-01
            • 2017-09-25
            相关资源
            最近更新 更多