【问题标题】:How to update Dropzone URL Dynamically如何动态更新 Dropzone URL
【发布时间】:2020-01-28 14:48:47
【问题描述】:

我尝试了几种不同的方法,但我无法让它发挥作用。

我正在尝试在Change Dropzone url dynamically这里关注这个答案

我有一个包含各种字段的 for 和两个 dropzone 实例。一个工作正常,但是这个需要根据每个文件动态生成的链接将文件上传到保管箱。

我正在尝试更新基于来自服务器的 ajax 响应处理的每个文件的 dropzone url 参数。

我知道 ajax 可以正常工作并获取我需要的 URL。

jQuery(document).ready(function(){

    Dropzone.autoDiscover = false
    jQuery("#files-uploader").dropzone({        
        url: 'url.com',
        init: function(){   

            var dropzoneobject=this;
            this.on("processing", function (file) {
                jQuery.ajax({
                    url:vendor_ajax_object.ajax_url,
                    type:'POST',
                    data:'action=knpGetUploadLink&filename='+file['name'],                  
                    success: function(data){
                        //data = a URL returned from the server
                        dropzoneobject.options.url = data;
                    },
                    error: function(){

                    }
                });            
            });

        },
        headers: {
            'content-type': 'application/octet-stream'
        },
        success: function (file, response) {
            console.log(response);
            file.previewElement.classList.add("dz-success");
        },
        error: function (file, response) {
            console.log(response);
            file.previewElement.classList.add("dz-error");
        },
        // update the following section is for removing image from library
        addRemoveLinks: true,
        removedfile: function(file) {}

    });

});

将数据变量记录到控制台表明服务器已返回正确的 URL。 在 ajax 成功参数中记录 dropzoneobject.options.url 显示设置的正确 URL。 Dropzone 说它已成功上传,但开发工具中的网络选项卡显示发布到的 URL 是 url.com。

我认为我的时间在某个地方搞砸了。

任何建议都会很棒。

TIA。

【问题讨论】:

    标签: jquery dropzone


    【解决方案1】:

    看起来这一切都与时间有关。

    换出处理事件并将 autoProcessQueue 设置为 false。

    然后在 ajax 调用完成后触发 processQueue。

    jQuery(document).ready(function(){
    
    Dropzone.autoDiscover = false
    jQuery("#files-uploader").dropzone({        
        url: 'url.com',
        autoProcessQueue: false,
        init: function(){   
    
            var dropzoneobject=this;
            console.log(dropzoneobject);
            this.on("addedfile", function (file) {
            jQuery.ajax({
                    url:vendor_ajax_object.ajax_url,
                    type:'POST',
                    data:'action=knpGetUploadLink&filename='+file['name'],                  
                    success: function(data){
                        dropzoneobject.options.url = data;
                        dropzoneobject.processQueue();
                    },
                    error: function(){
    
                    }
                });
    
            });
        },
        headers: {
            'content-type': 'application/octet-stream'
        },
        success: function (file, response) {
            //console.log(response);
            file.previewElement.classList.add("dz-success");
        },
        error: function (file, response) {
            //console.log(response);
            file.previewElement.classList.add("dz-error");
        },
        // update the following section is for removing image from library
        addRemoveLinks: true,
        removedfile: function(file) {}
    
    });
    
    })
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-02-14
      • 1970-01-01
      • 2016-08-21
      • 1970-01-01
      相关资源
      最近更新 更多