How to get file name when user select a file via <input type=“file” />?

前端 未结 4 1905
你的背包
你的背包 2020-11-27 15:55

I\'ve seen similar questions before,which ends up with no solution,because of security reasons.

But today I see hostmonster has successfully implemented this,when I

4条回答
  •  挽巷
    挽巷 (楼主)
    2020-11-27 16:09

    You can get the file name, but you cannot get the full client file-system path.

    Try to access to the value attribute of your file input on the change event.

    Most browsers will give you only the file name, but there are exceptions like IE8 which will give you a fake path like: "C:\fakepath\myfile.ext" and older versions (IE <= 6) which actually will give you the full client file-system path (due its lack of security).

    document.getElementById('fileInput').onchange = function () {
      alert('Selected file: ' + this.value);
    };
    

提交回复
热议问题