c# - How to restrict file upload to JPG, PNG, and GIF in ASP.NET MVC 3 - Stack Overflow

I am using a simple input box <input type="file" > in a HTML form, and I want to enfor

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
Add a ment  | 

1 Answer 1

Reset to default 5

you 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条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信