var fu1 = document.getElementById("FileUpload1");
How can I get the filename of the fileupload control with id FileUpload1
?
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.
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.
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);"/>
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 %>");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With