I am using a simple input box <input type="file" />
in a HTML form, and I want to enforce that only JPG, PNG, and GIF files can be uploaded.
How can I do this?
I am using a simple input box <input type="file" />
in a HTML form, and I want to enforce that only JPG, PNG, and GIF files can be uploaded.
How can I do this?
Share Improve this question edited May 5, 2012 at 4:16 Adam Lear♦ 38.8k12 gold badges82 silver badges103 bronze badges asked May 4, 2012 at 4:16 AhsanAhsan 31 silver badge3 bronze badges 2- possible duplicate of HTML <input type='file'> apply a filter – shf301 Commented May 4, 2012 at 4:19
- @Ahsan I agree with Ravi. But please note, this only checks the name of the file being uploaded. The actual format or content of the file could still be anything. – john_science Commented May 4, 2012 at 23:33
1 Answer
Reset to default 5you can check this link, CodeProject: Image uploading
$file = $("#yourFileuploadID");
var $filePath = $.trim($file.val());
if ($filePath == "") {
alert("Please browse a file to upload");
return;
}
var $ext = $filePath.split(".").pop().toLowerCase();
var $allow = new Array("gif", "png", "jpg", "jpeg");
if ($.inArray($ext, $allow) == -1) {
alert("Only image files are accepted, please browse a image file");
return;
}
PS : It's better to have server side validation, it will be handy when javascript is disabled at client side. make sure you check for both
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745273446a4619891.html
评论列表(0条)