繁体   English   中英

jQuery检查文件上传是否为mp3

[英]jQuery check that file upload is an mp3

使用正则表达式来确保要通过input[type="file"]上传的input[type="file"]是MP3。 发生错误,我正在测试的文件没有通过。

谢谢你的帮助!

的HTML

<input type="file"/>

的JavaScript

var file = $input.find('input[type="file"]'),
    val  = file.val(),
    type = /^([a-zA-Z0-9_-]+)(\.mp3)$/;

submit.attr('disabled', 'disabled').addClass('deac');

if (!val){

    modal("The upload can't be empty.", true);
} else if (!type.test(val)){

    modal("The song must be in MP3 format.", true);

    /***********
     *
     * Getting caught here- even with a valid .mp3 file that _should_ pass the regex
     * in my test case using mamp as a localhost, val = C:\fakepath\1.mp3 
     *
     ***********/
} else {

    /* Success */
}

我认为最好的解决方案是使用方法mime()

这样,即使文件用a重命名。 Mp3不受规避。

我认为IE是唯一添加“ fakepath”前缀之类的内容的浏览器,因此类似的方法可能会更好:

type = /^(?:[A-Z]:\\fakepath\\)?([a-zA-Z0-9_-]+)(\.[Mm][Pp]3)$/;

暂无
暂无

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

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