简体   繁体   English

如何在ASP.NET MVC 3中将文件上传限制为JPG,PNG和GIF

[英]How to restrict file upload to JPG, PNG, and GIF in ASP.NET MVC 3

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. 我正在以HTML格式使用简单的输入框<input type="file" /> ,我想强制执行只能上传JPG,PNG和GIF文件的操作。

How can I do this? 我怎样才能做到这一点?

you can check this link, CodeProject: Image uploading 您可以检查此链接CodeProject:图像上传

  $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. PS:最好具有服务器端验证,如果在客户端禁用了javascript,它将很方便。 make sure you check for both 确保您同时检查

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM