Use jQuery to get the file input's selected filename without the path

后端 未结 14 807
醉话见心
醉话见心 2020-11-27 10:02

I used this:

$(\'input[type=file]\').val()

to get the file name selected, but it returned the full path, as in \"C:\\fakepath\\filename.doc

14条回答
  •  暖寄归人
    2020-11-27 10:48

    Here is how I do it, it works pretty well.

    In your HTML do:

    
    

    Then in your js file create a simple function:

    function fileSelect(id, e){
        console.log(e.target.files[0].name);
    }
    

    If you're doing multiple files, you should also be able to get the list by looping over this:

    e.target.files[0].name
    

提交回复
热议问题