在文件上传控件中上传图像时验证图像尺寸 [英] Validate image dimension when image uploading in file upload controls

查看:95
本文介绍了在文件上传控件中上传图像时验证图像尺寸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只为我的aspx页面中的图像设置了三个fileupload控件。我已验证该文件是否为图像。现在我需要检查每个文件上传控件中的图像尺寸必须是640 * 480 ...如何在页面回发到服务器之前检查尺寸是640 * 480?

I have a three fileupload controls only for images in my aspx page.. I have validate that file is image or not. Now i need to check that image dimensions must be 640*480 in every fileupload controls... How can i do check that dimensions are 640*480 before page postback to server?

推荐答案

System.Drawing.Image imgFile =
System.Drawing.Image.FromStream(FileUploader1.PostedFile.InputStream);
  if (imgFile.PhysicalDimension.Width < 640 || imgFile.PhysicalDimension.Height < 280)
  {
      Reponse.Write("<script>alert('invalid iamge')</script>")
  }





我还没有尝试过这段代码,但我盲目地认为它是应该工作,

所以试一试,如果有效,请去其他FileUpload控件!



好​​运!



I haven't tried out this code yet but it is my blind assumption that it should work,
so give it a try and if it works go for other FileUpload control !

Good Luck !


不幸的是,你无法确定客户端文件的大小(好吧,你不能手动将字节数组解析成图像文件格式 - 而这可能不是你所追求的)。您可以做什么(假设您正在使用HTML 5),使用AJAX和File API将文件发布到服务器,并在那里解码文件。



你可以使用的一个技巧,这是我谈到的一种处理另一个问题的方法,昨天 [ ^ ]使用 HttpHandler 为您进行检查。基本上,您将图像作为字节数据发布到 HttpHandler ,它会将图像解码为物理图像,并以与Krunal Rohit代码类似的方式对其进行测试,然后如果可以正确上传,则返回一个合适的值。



这种方法的缺点当然是你将数据发布到服务器两次,但那里是否可以用来缓解这种情况的技术(例如将上传的图像存储在 Session 对象中)。
Unfortunately, you cannot determine the dimensions of a file at the client side (well, you can't without parsing the byte array into the image file format manually - and that's probably not what you're after). What you could do (assuming you're using HTML 5), is post the file over to the server using AJAX and the File API, and have the file decode there.

One trick you could use, and this is something I talked about as a way of handling another problem, yesterday[^] is to use an HttpHandler to do the checking for you. Basically, you would post the image as byte data across to the HttpHandler which would decode the image into a physical image and test it in a similar way to Krunal Rohit's code, and then return an appropriate value if it can be uploaded properly.

The downside of this approach, of course, is that you are posting the data to the server twice, but there are techniques you could use to mitigate that (such as storing the uploaded image in the Session object).


这篇关于在文件上传控件中上传图像时验证图像尺寸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆