【问题标题】:How to change thumbnail src value in Dropzone.js?如何更改 Dropzone.js 中的缩略图 src 值?
【发布时间】:2014-05-23 20:28:08
【问题描述】:

我正在使用带有 jQ​​uery 的 Dropzone.js 将文件上传到服务器。上传文件后,我正在使用当前 url 生成一个“服务器端”文件名。

$('.dropzone').dropzone({
    init: function() {
        this.on('success', function(file) {
            var newname = generateServersideFilename(file.name); // this is my function
            // here I need a help to find the thumbnail <img> to set the 'src' attribute
        }
    }
});

如何找到当前缩略图img来设置src属性?

【问题讨论】:

  • 不是最好的方法,但可以工作:$('span:contains("'+file.name+'")').closest('.dz-preview').find('img').attr('src', newname); - 如果你知道更好的方法,请分享。

标签: javascript jquery dropzone.js


【解决方案1】:

这对我有用:

$('.dropzone').dropzone({
    init: function() {
        this.on('success', function(file) {
            var newname = generateServersideFilename(file.name); // this is my function
            // changing src of preview element
            file.previewElement.querySelector("img").src = newname;
        }
    }
});

dropzonejs 有几个使用 file.previewElement 的例子

【讨论】:

    【解决方案2】:

    这可以很简单

    this.on("success", function(file) {
    var mydropzone = this;
    mydropzone.emit("thumbnail", file, "images/x.jpg");
    });
    

    这是来自 dropzone 的 link

    【讨论】:

    • 这应该是正确的答案,我们不应该直接编辑属性,而是应该触发一个'thumbnail'事件。
    【解决方案3】:
    this.on("addedfile", function (file) {
    
       file.previewElement.querySelector("img").src = "music-box-outline.svg";
    
    });
    

    【讨论】:

      猜你喜欢
      • 2016-02-22
      • 2017-05-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 2016-02-19
      • 1970-01-01
      • 2015-06-26
      相关资源
      最近更新 更多