Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 open a ticket and attach a file in their backend.

It works both with firefox and IE(version 8 to be exact).

I've verified it's exactly client side scripting,no request is sent(with firebug).

Now,can we re-consider this question?

like image 416
user198729 Avatar asked Feb 03 '10 04:02

user198729


People also ask

How do I find the name of a file object?

File getName() method in Java with Examples The getName() method is a part of File class. This function returns the Name of the given file object. The function returns a string object which contains the Name of the given file object.


1 Answers

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); }; 
like image 89
Christian C. Salvadó Avatar answered Oct 04 '22 17:10

Christian C. Salvadó