2

In Plupload we can limit the file types (say, archive file) by setting the filters as shown below

// Specify what files to browse for
filters : [
       {title : "Zip files", extensions : "zip,avi"}
    ],

I have list of files, (for example X.A1, Y.A2, Z.A3, ...) in which the extension can range from A1 to AN (where N is an integer).

What I would like is to filter all files that have an extension which starts with a A and are followed by a integer. Something like

filters : [
           {title : "Start With A", extensions : "A\\d+"}
        ],

I cound try to use a filter as such

filters : [
           {title : "Start With A", extensions : "A1,A2,A3,A4"}
        ],

but I don't know how many extensions we might have.

Is there a way to use regular expressions to filter the selected files?

Luís Cruz
  • 14,780
  • 16
  • 68
  • 100
RameshPasa
  • 407
  • 6
  • 25

1 Answers1

1

You have two options:

  1. You just need to create a string of extensions you want to allow. A simple for loop is enough to create such string.

    Using this solution you set the correct allowed extensions.

  2. You allow the user to select any file extension but, after a file is selected, you verify if the extension matches your requirements. If it does, then add the file to the upload queue. If it doens't, throw an error to the user.

Option 1:

var extensions = '', i;

for (i = 1; i < 30; i++) {
    extensions += 'A' + i + ',';
}
extensions = extensions.slice(0, -1);

var uploader = new plupload.Uploader({
    // (....)

    filters : {
        max_file_size : '10mb',
        mime_types: [
            {title : "Custom files", extensions: extensions}
        ]
    },
    
    // (....)

Option 2:

var uploader = new plupload.Uploader({
    // (....)

    init: {
        FilesAdded: function(up, files) {
            // Foreach file
            plupload.each(files, function(file) {
                // Get the file extension
                var fileExtension = file.name.split('.').pop();
                // Create the pattern
                var pattern = new RegExp("^A\d$");

                // If the pattern doesn't match, don't allow the file.
                if (!pattern.test(fileExtension)) {
                    console.log('not added');
                    return false;
                }

                console.log('added');
                document.getElementById('filelist').innerHTML += '<div id="' + file.id + '">' + file.name + ' (' + plupload.formatSize(file.size) + ') <b></b></div>';
            });
        },
        
    // (....)
Community
  • 1
  • 1
Luís Cruz
  • 14,780
  • 16
  • 68
  • 100
  • Can't try option1 as we don't know how many extensions will be there.As for option2 we have done that validation but looking for filtering file when the file browser is shown. We were able to do that in chrome browser but not in IE and FF. – RameshPasa Jun 01 '16 at 05:30