简体   繁体   中英

jQuery check that file upload is an mp3

Using a regex to make sure a file that's being uploaded through input[type="file"] is an MP3. Somethings going wrong, the files that I'm testing with arn't passing.

Thanks for your help!

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 */
}

I think the best solution is to use the method mime() .

In this way, even if the file is renamed with a. Mp3 is not circumvented control.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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