简体   繁体   English

JavaScript 检查多个上传的文件扩展名

[英]JavaScript check file extension for multiple uploads

I have contact form, and Js which validates the size of selected files.我有联系表格和验证所选文件大小的 Js。 Also I want to add extension check.另外我想添加扩展检查。 My code so far is到目前为止我的代码是

var inputs = $('input[type="file"]')
inputs.on('change', function () {
    var size = 0;

    inputs.each(function () {
        $.each(this.files, function () {
            size += this.size;
            var names = [];
           
            alert(x);

        });
    });

    if (size > 19000000) {
        alert('Ukupna dozvoljena veličina fajlova za upload je 20mb!');
        $('.inputDugme').attr('disabled', 'disabled');
    } else {
        $('.inputDugme').removeAttr('disabled', 'disabled')
    }
});

Is there I way to get extension of files, and save it in array.有没有办法获取文件的扩展名,并将其保存在数组中。 And then check content of array.然后检查数组的内容。

You can collect extension names like this:您可以像这样收集扩展名:

var size = 0,
    etx = [];

inputs.each(function () {
    $.each(this.files, function () {
        size += this.size;
        ext.push(this.name.split('.').pop());
    });
});

x[] = this.name; is not valid syntax in Javascript, you should use Array.prototype.push method.在 Javascript 中不是有效语法,您应该使用Array.prototype.push方法。

尝试这个。

var ext = filename.split('.')[filename.split('.').length - 1];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM