Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the filename of a fileupload in a document through JavaScript

var fu1 = document.getElementById("FileUpload1"); 

How can I get the filename of the fileupload control with id FileUpload1?

like image 226
Mishigen Avatar asked Nov 26 '09 16:11

Mishigen


People also ask

How do I get the file extension of an input type file?

The full filename is first obtained by selecting the file input and getting its value property. This returns the filename as a string. By the help of split() method, we will split the filename into 2 parts. The first part will be the filename and the second part will be the extension of the file.

How can get upload file name in PHP?

In PHP, we can access the actual name of the file which we are uploading by keyword $_FILES[“file”][“name”]. The $_FILES is the by default keyword in PHP to access the details of files that we uploaded. The file refers to the name which is defined in the “index. html” form in the input of the file.


2 Answers

In google chrome element.value return the name + the path, but a fake path. Thus, for my case I used the name attribute on the file like below :

function getFileData(myFile){    var file = myFile.files[0];      var filename = file.name; } 

this is the call from the page :

<input id="ph1" name="photo" type="file" class="jq_req" onchange="getFileData(this);"/> 
like image 152
vanessen Avatar answered Sep 28 '22 03:09

vanessen


Try the value property, like this:

var fu1 = document.getElementById("FileUpload1"); alert("You selected " + fu1.value); 

NOTE: It looks like FileUpload1 is an ASP.Net server-side FileUpload control.
If so, you should get its ID using the ClientID property, like this:

var fu1 = document.getElementById("<%= FileUpload1.ClientID %>"); 
like image 25
SLaks Avatar answered Sep 28 '22 03:09

SLaks