Plupload - 仅限于一个文件

问题描述 投票:23回答:13

我没有在plupload API文档中看到限制上传文件数量的选项,任何数字,甚至1。

Doc失败了吗?或功能失败?如果它不存在,我会努力让这种情况发生,如果有人需要它..

javascript plupload
13个回答
20
投票

这是一个功能失败。我围绕jQuery API创建了一个包装器,这就是我为使它工作所做的。我的代码做了一些其他业务逻辑,但它应该足以让你设置启动。

基本上,绑定到FilesAdded事件并在上传器对象上调用removeFile(如果文件太多)。我想我添加了50ms超时,因为它给我带来了尚未存在的文件的问题。

uploader.bind('FilesAdded', function (up, files) {
    var i = up.files.length,
        maxCountError = false;

    plupload.each(files, function (file) {

        if(uploader.settings.max_file_count && i >= uploader.settings.max_file_count){
            maxCountError = true;
            setTimeout(function(){ up.removeFile(file); }, 50);
        }else{
            // Code to add pending file details, if you want
        }

        i++;
    });

    if(maxCountError){
        // Too many files uploaded, do something
    }

});

max_file_count是我在创建它时添加到pluploader实例的东西。


编辑:我实际上必须为此做两种不同的方式。以上只允许一个人上传一定数量的文件(否则会产生错误)。

此代码段的工作方式类似 - 但会删除现有文件并仅上传最新的文件:

uploader.bind('FilesAdded', function (up, files) {
    var fileCount = up.files.length,
        i = 0,
        ids = $.map(up.files, function (item) { return item.id; });

    for (i = 0; i < fileCount; i++) {
        uploader.removeFile(uploader.getFile(ids[i]));
    }

    // Do something with file details
});

1
投票

我意识到这已经得到了回答,但我采用的解决方案是使用QueueChanged回调:

QueueChanged: function(uploader) {
    // Called when queue is changed by adding or removing files
    //log('[QueueChanged]');
    if(uploader.files.length > 1) {
        uploader.files.splice(0, (parseInt(uploader.files.length) - 1));
    }
}

使用此代码,它只保留最后选择的文件(如果他们再次选择的原因是因为他们选择了错误的文件)。


1
投票

如果您想使用自定义上传器,并一次上传一个文件,没有自动上传并添加最后一个文件成为新文件使用此文件。

uploader.bind('FilesAdded', function(up, files) {
    // Clear the HTML
    $('#plupload-files').html('');

    // Plup does not offer before file added event
    if (up.files.length > 1) {
        up.splice(0, up.files.length);
        up.addFile(files[0])
        return;
    }

    // $.each(files, function(){....
}

我们拼接整个数组因为plup已经将所有文件添加到队列中,如果你像接受的答案一样拼接队列,那么每次用户尝试添加文件时你实际上会添加一个文件,如果他们试图单独添加一个新文件将旧文件保存在文件数组中的pos [0],

然后我们添加他们尝试添加的文件的第一个文件。这样,队列中只有一个文件,它始终是他们尝试添加的最后一组文件中的第一个文件。

即。

拖入plupload'file1.jpg','file2.jpg','file3.jpg'

清除整个队列,加回'file1.jpg'

拖入plupload'file4.jpg','file5.jpg','file6.jpg'

清除整个队列,加回'file4.jpg'

拖入plupload'file99.jpg'

清除整个队列,加回'file99.jpg'

如果您只想一次上传一个文件,则可以管理自定义选项。如上所述,其他答案只能使用一次,或者使用自动启动上传。


0
投票

上传前直接删除不必要的文件:

$('uploadfiles').onclick = function()
            {
                while (uploader.files.length > 1)
                {
                        uploader.removeFile(uploader.files[0]);
                }

                uploader.start();
                return false;
            };

0
投票

在尝试了每个解决方案之后,我想出了最简单的解决方案 - 这似乎有效。

我正在使用核心api,我将multi_selection设置为false。然后,在选择(添加)第一个文件后,我插入FilesAdded事件的第二行代码隐藏了浏览链接。我不认为这可以用jquery小部件来完成,我也发现除非上传链接覆盖了浏览链接的位置,否则它仍然存在。

uploader.bind('FilesAdded', function(up, files) {
//line below hides button
document.getElementById("browse").style.display = "none";

var html = '';
plupload.each(files, function(file) {
html += '<li id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></li>';
});
document.getElementById('filelist').innerHTML += html;

});


20
投票

其他方法:

 $(document).ready(function () {
    var uploader = new plupload.Uploader({
        ...
        multi_selection: false,
       ....
     });

问候


13
投票

基于Jonathon Bolster的第二个答案,我编写了这个更简单的片段来限制上传到最后选择的文件:

uploader.bind('FilesAdded', function(up, files) {
    while (up.files.length > 1) {
        up.removeFile(up.files[0]);
    }
});

6
投票

你可以使用这个max_file_count: 5,其中5是最大上传次数。


3
投票

为什么不呢

    if (files.length > 1) uploader.splice(1, files.length - 1);

2
投票

试试这个。这对我来说可以。

uploader.bind('FilesAdded', function(up, files) {

if(uploader.files.length > 1)
{
    uploader.removeFile(uploader.files[0]);
    uploader.refresh();// must refresh for flash runtime
}

。 。 。休息

想法是测试当前上传者对象中的num文件。如果length大于1,那么只需使用方法uploader.removeFile。请注意,参数是files[0],它不是文件ID,而是完整的文件对象。

({id:"p17bqh1v4dp42gdf1gan75p16tp3", name:"gnome-globe.png", size:48456, loaded:0, percent:0, status:1})

最好的祝福!


2
投票

现在,您可以通过将multi_selection选项设置为false来禁用多选

像这样

var uploader = new plupload.Uploader({
    runtimes : 'html5,flash',
    browse_button : 'button_id',
    multi_selection:false,  //disable multi-selection
...

官方文件在这里:http://www.plupload.com/documentation.php


2
投票
    FilesAdded: function(up, files) {
        up.files.splice(0,up.files.length-1);
    },
    multi_selection: false,

使用up.files,只需filesfiles将始终包含我们从文件浏览器中选择的单个项目。 up.files是我们需要缩小到上一个选定文件的实际列表。


1
投票

只允许上传一个文件:

uploader.bind('FilesAdded', function(up, files) {
    $.each(files, function(i, file) {
        if(uploader.files.length!=1){uploader.removeFile(file); return;}
    });
});

允许一次选择一个文件:

uploader.bind('FilesAdded', function(up, files) {
    $.each(files, function(i, file) {
        if(i){up.removeFile(file); return;}
    });
});

允许一次上传一个文件:

uploader.bind('FilesAdded', function(up, files) {
    $.each(files, function(i, file) {
        if(uploader.files.length!=1){uploader.removeFile(file); return;}
    });
});
uploader.bind('FileUploaded', function(up, file,response) {
    up.removeFile(file);
});
© www.soinside.com 2019 - 2024. All rights reserved.