jQuery獲取上傳文件的名稱


 

//獲取文件名稱
function getFileName(path) {
    var pos1 = path.lastIndexOf('/');
    var pos2 = path.lastIndexOf('\\');
    var pos = Math.max(pos1, pos2);
    if (pos < 0) {
        return path;
    }
    else {
        return path.substring(pos + 1);
    }
}
 
$(document).ready(function () {
    $('#file').change(function () {
        var str = $(this).val();
        var fileName = getFileName(str);
        var fileExt = str.substring(str.lastIndexOf('.') + 1); 
        alert(fileName + "\r\n" + fileExt);
    });
});

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM