I'm using Blueimp jQuery file upload plugin for upload files.

I had no problem in uploading but the option maxFileSize and acceptFileTypes do not work.

This is my code:

$(document).ready(function () {
    'use strict';

    $('#fileupload').fileupload({
        dataType: 'json',
        autoUpload: false,
        acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
        maxFileSize: 5000000,
        done: function (e, data) {
            $.each(data.result.files, function (index, file) {
                $('<p style="color: green;">' + file.name + '<i class="elusive-ok" style="padding-left:10px;"/> - Type: ' + file.type + ' - Size: ' + file.size + ' byte</p>')
                    .appendTo('#div_files');
            });
        },
        fail: function (e, data) {
            $.each(data.messages, function (index, error) {
                $('<p style="color: red;">Upload file error: ' + error + '<i class="elusive-remove" style="padding-left:10px;"/></p>')
                    .appendTo('#div_files');
            });
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);

            $('#progress .bar').css('width', progress + '%');
        }
    });
});

推荐答案

有同样的问题,blueimp的家伙说"maxFileSize and acceptFileTypes are only supported by the UI version",并提供了一个(断开的)链接来合并_validate和_hasError方法.

So without knowing how to incorporate those methods without messing up the script I wrote this little function. It seems to work for me.

Just add this

add: function(e, data) {
        var uploadErrors = [];
        var acceptFileTypes = /^image\/(gif|jpe?g|png)$/i;
        if(data.originalFiles[0]['type'].length && !acceptFileTypes.test(data.originalFiles[0]['type'])) {
            uploadErrors.push('Not an accepted file type');
        }
        if(data.originalFiles[0]['size'].length && data.originalFiles[0]['size'] > 5000000) {
            uploadErrors.push('Filesize is too big');
        }
        if(uploadErrors.length > 0) {
            alert(uploadErrors.join("\n"));
        } else {
            data.submit();
        }
},

at the start of the .fileupload options as shown in your code here

$(document).ready(function () {
    'use strict';

    $('#fileupload').fileupload({
        add: function(e, data) {
                var uploadErrors = [];
                var acceptFileTypes = /^image\/(gif|jpe?g|png)$/i;
                if(data.originalFiles[0]['type'].length && !acceptFileTypes.test(data.originalFiles[0]['type'])) {
                    uploadErrors.push('Not an accepted file type');
                }
                if(data.originalFiles[0]['size'].length && data.originalFiles[0]['size'] > 5000000) {
                    uploadErrors.push('Filesize is too big');
                }
                if(uploadErrors.length > 0) {
                    alert(uploadErrors.join("\n"));
                } else {
                    data.submit();
                }
        },
        dataType: 'json',
        autoUpload: false,
        // acceptFileTypes: /(\.|\/)(gif|jpe?g|png)$/i,
        // maxFileSize: 5000000,
        done: function (e, data) {
            $.each(data.result.files, function (index, file) {
                $('<p style="color: green;">' + file.name + '<i class="elusive-ok" style="padding-left:10px;"/> - Type: ' + file.type + ' - Size: ' + file.size + ' byte</p>')
                .appendTo('#div_files');
            });
        },
        fail: function (e, data) {
            $.each(data.messages, function (index, error) {
                $('<p style="color: red;">Upload file error: ' + error + '<i class="elusive-remove" style="padding-left:10px;"/></p>')
                .appendTo('#div_files');
            });
        },
        progressall: function (e, data) {
            var progress = parseInt(data.loaded / data.total * 100, 10);

            $('#progress .bar').css('width', progress + '%');
        }
    });
});

您会注意到,我在其中还添加了一个filesize函数,因为这也只适用于UI版本.

更新以通过@lopsided建议的问题:在查询中添加了data.originalFiles[0]['type'].lengthdata.originalFiles[0]['size'].length,以确保它们存在,并且在测试错误之前不为空.如果它们不存在,则不会显示错误,并且只依赖于服务器端错误测试.

Jquery相关问答推荐

为什么如果使用转换规模,juserui可拖动遏制不起作用

JQuery日期 Select 器未设置从jQuery中 Select 的月份起90天或3个月

JQuery DateTimePicker-在 Select 中抓取星期几

多个 AJAX 调用;获取所有失败的呼叫

通过 jQuery 提取 application/json 数据

ASP.NET MVC - Ajax 将空值传递给控制器

禁用提交按钮,直到所有字段都有值

foreach for JSON 数组,语法

jQuery 绑定点击 *ANYTHING* 但 *ELEMENT*

有没有办法放大 D3 力布局图?

jQuery用另一个类替换一个类

如何判断元素是否在屏幕外

点击时保持 Bootstrap 下拉菜单打开

好处和.在本地托管 jQuery 的trap

jQuery 和 jQuery Mobile 的区别?

如何使 jQuery Contains 不区分大小写,包括 jQuery 1.8+?

使用 jQuery 比较两个 Javascript 对象数组

jQuery:如何找到父母的特定子元素?

如何将参数传递给 jQuery 中的事件处理程序?

$.getJSON 在 IE8 中返回缓存数据