最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - DropzoneJS limit selecting of files to one file but allow subsequent uploads - Stack Overflow

programmeradmin2浏览0评论

I am trying to limit the number of files a user can select to one using the option maxFiles: 1; however that also prevents the user from uploading a second, third etc. file which is what I want. I want only the selection to be limited to one file and allow for subsequent uploads. Is that possible?

Here is my code:

$(function() {
    var dropzone = new Dropzone('#avatar-wrapper', {
        url: '/uploads/avatar',
        clickable: '.upload',
        maxFilesize: 5,
        maxFiles: 1,
        previewsContainer: false,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        init: function() {
            this.on('addedfile', function(file) {
                console.log('test');
                $('#loader').show();
            });

            this.on('success', function(file, result) {
                $('#avatar_url').val(result.url);
                $('#avatar').attr('src', result.url);
                $('#loader').hide();
            });
        }
    });
});

I am trying to limit the number of files a user can select to one using the option maxFiles: 1; however that also prevents the user from uploading a second, third etc. file which is what I want. I want only the selection to be limited to one file and allow for subsequent uploads. Is that possible?

Here is my code:

$(function() {
    var dropzone = new Dropzone('#avatar-wrapper', {
        url: '/uploads/avatar',
        clickable: '.upload',
        maxFilesize: 5,
        maxFiles: 1,
        previewsContainer: false,
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        },
        init: function() {
            this.on('addedfile', function(file) {
                console.log('test');
                $('#loader').show();
            });

            this.on('success', function(file, result) {
                $('#avatar_url').val(result.url);
                $('#avatar').attr('src', result.url);
                $('#loader').hide();
            });
        }
    });
});
Share Improve this question edited Jul 5, 2018 at 11:12 Petar Vasilev asked May 16, 2018 at 9:46 Petar VasilevPetar Vasilev 4,7556 gold badges47 silver badges78 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8 +25

Instead of patching the DropZone library, you could do this change at runtime after you initialized the DropZone element.

var dropzone = new DropZone('#avatar-wrapper', {
    // options ...
});
dropzone.hiddenFileInput.removeAttribute("multiple");

Found this in the dropzone.js file:

if (_this3.options.maxFiles === null || _this3.options.maxFiles > 1) {
    _this3.hiddenFileInput.setAttribute("multiple", "multiple");
}

and mented out the second line:

if (_this3.options.maxFiles === null || _this3.options.maxFiles > 1) {
    //_this3.hiddenFileInput.setAttribute("multiple", "multiple");
}

this solved my problem; however it would be nice to have this implemented in DropzoneJs

发布评论

评论列表(0)

  1. 暂无评论