admin管理员组文章数量:1026989
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
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
本文标签: cHow to restrict file upload to JPGpngand GIF in ASPNET MVC 3Stack Overflow
版权声明:本文标题:c# - How to restrict file upload to JPG, PNG, and GIF in ASP.NET MVC 3 - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745273444a2142953.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论