Plupload - Restrict to only one file

后端 未结 13 1013
攒了一身酷
攒了一身酷 2021-02-02 10:15

I don\'t see an option in the plupload API docs on restricting the number of files uploaded, to any number, even 1.

Doc fail? or Feature fail? If it doesn\'t exist I\'ll

相关标签:
13条回答
  • 2021-02-02 11:09

    Allow only one file to be uploaded:

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

    Allow one file to be selected at once:

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

    Allow one file to upload at once:

    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);
    });
    
    0 讨论(0)
  • 2021-02-02 11:09

    Remove unnecessary files directly before uploading:

    $('uploadfiles').onclick = function()
                {
                    while (uploader.files.length > 1)
                    {
                            uploader.removeFile(uploader.files[0]);
                    }
    
                    uploader.start();
                    return false;
                };
    
    0 讨论(0)
  • 2021-02-02 11:11

    Based on Jonathon Bolster's second answer, I wrote this simpler snippet to restrict upload to the last selected file:

    uploader.bind('FilesAdded', function(up, files) {
        while (up.files.length > 1) {
            up.removeFile(up.files[0]);
        }
    });
    
    0 讨论(0)
  • 2021-02-02 11:11

    Why not just

        if (files.length > 1) uploader.splice(1, files.length - 1);
    
    0 讨论(0)
  • 2021-02-02 11:11

    Try this. It works fine for me.

    uploader.bind('FilesAdded', function(up, files) {
    
    if(uploader.files.length > 1)
    {
        uploader.removeFile(uploader.files[0]);
        uploader.refresh();// must refresh for flash runtime
    }
    

    . . . resto

    Idea is to test num files in current uploader object. If length is greater than 1, then just use method uploader.removeFile. Notice that argument is files[0] which is not a file id, but complete file object.

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

    Best regards!

    0 讨论(0)
  • 2021-02-02 11:12
        FilesAdded: function(up, files) {
            up.files.splice(0,up.files.length-1);
        },
        multi_selection: false,
    

    use up.files, just files. files will always contain single item what we select from file browser. up.files is the actual list which we need to shrink to last selected file.

    0 讨论(0)
提交回复
热议问题