如何使用jQuery文件上传接受.csv,.xsl,.xslx文件?

时间:2015-10-15 20:03:37

标签: jquery file upload blueimp

我正在尝试使用它https://github.com/blueimp/jQuery-File-Upload

我用PHP实现了这个来管理上传的文件,一切正常,我想限制文件类型,上传者需要接受的是.CSV,.XSL,.XSLX

我不知道如何配置它,因为没有关于接受这些扩展的文件类型的解释。

@ 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 + '%'
            );
        }
    });

3 个答案:

答案 0 :(得分:1)

编辑:

我在demo websites来源上找到了以下内容:

$.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'
        }
    },

可以找到选项的文档here

修改

我发现你需要使用基本插件再增加2个文件。 jquery.fileupload-process.js jquery.fileupload-validate.jshere。然后修改validate.js文件以仅接受所需的文件扩展名。可以找到所有这些文档https://stackoverflow.com/a/33148242/895245您应该阅读文档!

答案 1 :(得分:1)

您可以使用以下代码行检查PHP中的文件类型:

function upload_file(){
  $types = array('text/csv', 'application/vnd.ms-excel', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

  if(in_array($_FILES['uploaded_filename']['type'], $types)){
      //Manage the uploaded file here
  } else {
      //Handle the excluded filetypes here
}

在这里,您可以看到完整的MIME类型列表:http://www.freeformatter.com/mime-types-list.html

答案 2 :(得分:0)

我认为这是正在执行validation的文件。它实际上扩展了主插件。