Search code examples
jqueryfileuploadblueimp

How to accept .csv, .xsl, .xslx files using jQuery file upload?


I am trying to use it https://github.com/blueimp/jQuery-File-Upload

I've implemented this with PHP to manage uploaded files, everything works, I want to limit file type, the uploader needs to accept are .CSV, .XSL, .XSLX

I don't know how to configure it since there no explanation about file types to accept these extensions.

Snippet for @c25

       $('#fileupload').fileupload({
        url: url,
        dataType: 'json',
        done: function (e, data) {
            // Add each uploaded file name to the #files list
            $.each(data.result.files, function (index, file) {
                $('<li/>').text(file.name).appendTo('#files');
            });
        },
        progressall: function (e, data) {
            // Update the progress bar while files are being uploaded
            var progress = parseInt(data.loaded / data.total * 100, 10);
            $('#progress .bar').css(
                'width',
                progress + '%'
            );
        }
    });

Solution

  • EDIT:

    I found the following on the demo websites source:

    $.widget('blueimp.fileupload', $.blueimp.fileupload, {
    
        options: {
    
            // The regular expression for allowed file types, matches
            // against either file type or file name:
            acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
            // The maximum allowed file size in bytes:
            maxFileSize: 10000000, // 10 MB
            // The minimum allowed file size in bytes:
            minFileSize: undefined, // No minimal file size
            // The limit of files to be uploaded:
            maxNumberOfFiles: 10,
            */
    
            // Function returning the current number of files,
            // has to be overriden for maxNumberOfFiles validation:
            getNumberOfFiles: $.noop,
    
            // Error and info messages:
            messages: {
                maxNumberOfFiles: 'Maximum number of files exceeded',
                acceptFileTypes: 'File type not allowed',
                maxFileSize: 'File is too large',
                minFileSize: 'File is too small'
            }
        },
    

    The documentation for the options can be found here.

    EDIT

    I found that you will need 2 more files with the basic plugin. The jquery.fileupload-process.js and jquery.fileupload-validate.js. Then modify the validate.js file to accept only the file extensions you need. The documentation for all this can be found here. You should really read the documentation!