Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if user selected a file for file upload?

If I have a

<input id="uploadFile" type="file" /> 

tag, and a submit button, how do I determine, in IE6 (and above) if a file has been selected by the user.

In FF, I just do:

var selected = document.getElementById("uploadBox").files.length > 0; 

But that doesn't work in IE.

like image 561
slolife Avatar asked Sep 05 '08 16:09

slolife


People also ask

How do you check file is selected or not in JS?

length property to check file is selected or not. If element. files. length property returns 0 then the file is not selected otherwise file is selected.

How do I find the upload file name?

Try document. getElementById("FileUpload1"). value this value should have a path for a file to be uploaded, just strip all dirs from that value and you will have file name.


2 Answers

This works in IE (and FF, I believe):

if(document.getElementById("uploadBox").value != "") {    // you have a file } 
like image 195
Jason Bunting Avatar answered Sep 21 '22 19:09

Jason Bunting


this piece of code works in my local environment, hope it will also works in live

var nme = document.getElementById("uploadFile"); if(nme.value.length < 4) {     alert('Must Select any of your photo for upload!');     nme.focus();     return false; } 
like image 22
Atiqur Avatar answered Sep 18 '22 19:09

Atiqur