Uploading file.. can javascript detect file type and size before form is submitted?

hi,may i know whether javascript manage to check file size and file type once the user select the file from his local computer before the upload form is submitted?

You can read (but not write to) the value of the file selected from the input.value, and examine the extension, but you can’t read the visitor’s filesystem to get any of the file’s properties, with javascript.

oh,let say if the user selected a file “…/photo.jpg”

How can i check whether the file is “.jpg” or .“gif”,then if it is “.jpg” or .“gif”, do nothing. Else alert wrong file type…

I’m quite new to javascript…
how to write code to examine the “…/photo.jpg” part.
the method that i know is only like

document.formname.fieldname.value=“the_value”

var str=document.formname.fieldname.value;
if(/\.(jpg)|(gif)$/.test(str)){
//do what you want if the file is a gif or jpg
// You can set the value to ‘’,to prevent the upload
}
else //do what you do for other files

mrhoo,

really thanks for your help!!!
thanks!!! :wink:

var str = "some/something/photo.gif";
[COLOR="Green"]//or var str = document.getElementById("theID").value[/COLOR]
var pos = str.lastIndexOf(".");
var ext = str.substr(pos);
switch (ext)
{
case ".jpg":
	alert("jpg"); [COLOR="Green"]//or do something else[/COLOR]
	break;
case ".gif":
	alert("gif"); [COLOR="Green"]//or do something else[/COLOR]
	break;
default:
	alert("not a common image format");
}

7stud,
thanks for your suggestion too…
:wink: