簡體   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