Dropzone:动态更改acceptedFiles [英] Dropzone: change acceptedFiles dynamically

查看:86
本文介绍了Dropzone:动态更改acceptedFiles的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试更改以前创建的Dropzone的acceptedFiles选项.

I'm trying to change the acceptedFiles Option of a previously created Dropzone.

这是我设置选项的方式:

This is how I set the Options:

$myDropzone.options.acceptedFiles = '.jpg, .png';

这是生成的Dropzone:

and this is the generated Dropzone:

var $myDropzone = $("#my-dropzone");
$myDropzone.dropzone({
    maxFiles: 100,
    maxFilesize: 32,
    acceptedFiles: ".jpg"});

产生的错误是:

TypeError:$ myDropzone.options未定义

TypeError: $myDropzone.options is undefined

更新:

似乎没有运行任何错误:

Seems like this is not running any errors:

$myDropzone.options = { acceptedFiles: '.jpg, .png' };

但是接受新文件类型也没有任何改变.

But there is also no change in accepting the new filetype.

这里是操场示例: http://codepen.io/anon/pen/GqqLGo

推荐答案

使用Dropzone文档中提到的accept回调函数可以起作用,但它并不限制用户文件浏览器中显示的文件类型.如果没有选择的文件扩展名,它只会在用户选择后丢弃该文件.相反,请完成以下步骤.

Using the accept callback function mentioned in Dropzone's documention could work, but it doesn't restrict the file types displayed in the user's file explorer. It only discards the file after user selection if it doesn't have an accepted file extension. Instead, complete the following steps.

  1. 在代码中某处的隐藏输入上更改accept属性,以使其在初始化dropzone之后但用户可以使用dropzone之前触发.这将限制用户可以选择的文件类型.

  1. Change the accept attribute on the hidden input somewhere in your code so that it fires after the dropzone is initialized, but before the dropzone can be used by a user. This will limit the file types a user can select from.

$('.dz-hidden-input').attr('accept', '.pdf, .doc, .docx');
var mediaType = 'document';

  • 在用户添加文件以告知Dropzone可以接受哪些文件类型之后,更改acceptedFiles选项.从技术上讲,可以将其添加到发送文件之前触发的任何Dropzone事件回调中.

  • Change the acceptedFiles option after a user adds a file to tell Dropzone what file types may be accepted. This technically could be added to any Dropzone event callback that fires before sending the file.

    $('#dropzone').dropzone({
        acceptedFiles: 'image/*',
        init: function () {
            this.on('addedfile', function (file, xhr, formData) {
                if (mediaType == 'document') {
                    this.options.acceptedFiles = '.pdf, .docx, .doc';
                }
            });
        }
    });
    

  • 这篇关于Dropzone:动态更改acceptedFiles的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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