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

user198729提出了一个问题:How to get file name when user select a file via ?,或许与您遇到的问题类似。

回答者CMS给出了该问题的处理方式:

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);
};

希望本文对你有帮助,欢迎支持JavaScript中文网