以编程方式将现有文件添加到Dropzone [英] Programmatically Add Existing File to Dropzone

查看:74
本文介绍了以编程方式将现有文件添加到Dropzone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 dropzone.js常见问题解答作为指南:

I'm trying to add an existing image to my dropzone programmatically, using the dropzone.js FAQ as a guide:

// Add the existing image if it's there.
// headerDropzone is my dropzone (debug shows it as existing and initialized at this point.
var on_load_header = $( '[name="on_load_header_image"]' ).val();
var on_load_header_path = $( '[name="on_load_header_image_path"]' ).val();

if ( on_load_header ) {

    // Hardcoded size value is just for testing, see my second question below.
    var on_load_header_data = { name: on_load_header, size: 12345 };

    // Call the default addedfile event handler
    headerDropzone.options.addedfile.call( headerDropzone, on_load_header_data );

    // And optionally show the thumbnail of the file:
    headerDropzone.options. thumbnail.call( headerDropzone, on_load_header_data, on_load_header_path);

}

我的第一个问题是,这只是行不通。触发(或至少不触发 headerDropzone 中的addedfile处理程序),缩略图也是如此

My first problem is that this is just not working. The addedfile event doesn't fire (or at least the addedfile handler in headerDropzone never fires), same goes for thumbnail.

我的第二个问题/问题是:我是否必须提供文件大小?我可以在服务器端使用它,但是如果我实际上不需要的话,我宁愿不这样做。

My second problem/question is: do I have to provide the file size? I could get it server side, but I'd rather not do it if I don't actually need to.

推荐答案

请参阅如果实际上定义了函数 headerDropzone.options.addedfile headerDropzone.options.thumbnail 。它应该按照您的方式工作,但是如果没有更多信息,很难说出问题所在。

See if the functions headerDropzone.options.addedfile and headerDropzone.options.thumbnail are actually defined. It should work the way you did it, but without further info it's difficult to tell what's wrong.

关于文件大小:不,实际上没有必要提供准确的文件大小。只是Dropzone自动显示文件大小。如果您不关心是否显示了错误的文件大小,则可以提供一些随机数或 0 。否则,您可能要在添加文件后使用CSS或JS隐藏文件大小。 (有问题的元素的类为 dz-size

About the filesize: No, it's not necessary to actually provide the accurate filesize. It's just that Dropzone automatically displays the filesize. If you don't care if some false filesize is displayed then you can just provide some random number or 0. Otherwise you might want to hide the filesize with CSS, or with JS after you add it. (The element in question has the class dz-size.

其JavaScript版本如下所示) :

The JavaScript version of it would look something like this:

var fileSizeElement = on_load_header_data.previewElement.querySelector(".dz-size");
fileSizeElement.parentNode.removeChild(fileSizeElement);

这篇关于以编程方式将现有文件添加到Dropzone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆