检查文件上传时图像的高度和宽度 [英] Checking Height and Width of an Image on File Upload

查看:63
本文介绍了检查文件上传时图像的高度和宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hiya,

我正在使用FileUpload使用以下代码为我的应用程序上传图像:

Hiya,

I am using a FileUpload to upload images for my application using the following code:

string virtualFolder = "~/Album/";
            string physicalFolder = Server.MapPath(virtualFolder);
            string fileName = Guid.NewGuid().ToString();
            string extension = System.IO.Path.GetExtension(imageUpload.FileName);

            imageUpload.SaveAs(System.IO.Path.Combine(physicalFolder, fileName + extension));



在执行SaveAs命令并保存文件之前,是否有任何方法可以获取图像的宽度和高度,并在图像的高度或宽度大于给定值时阻止其上载?



Is there any way, before the SaveAs command is executed and the file is saved, to get the width and height of the image and preventing it from being uploaded if it is higher or wider than given values?

Any help would be excellent!

推荐答案

文件上传之前"是什么意思?在上传之前,它不作为完整的数据存在,不在服务器端.仅当上传文件时,您才能尝试将其作为位图打开,然后查看是否可以将其作为有效的位图文件打开.如果成功(通过未捕获异常来检测),则可以找出图像的大小(以像素为单位).在这种情况下,您可以选择删除它.

例如,您可以使用System.Drawing.Bitmap.

使用此构造函数打开文件: http://msdn.microsoft.com/en-us/library/0cbhe98f.aspx [ ^ ].

使用此属性可以找到大小: http://msdn.microsoft.com /en-us/library/system.drawing.image.size.aspx [
What do you mean "before the file is uploaded"? Before it is uploaded, it does not exist as an integral piece of data, not on the server side. Only when a file is uploaded, you can try to open it as a bitmap and see if it can be opened as a valid bitmap file. In case of success (which is detected by not catching an exception) you can find out the size of the image in pixels. In this case, you may choose to delete it.

For example, you can do it using System.Drawing.Bitmap.

Use this constructor to open the file: http://msdn.microsoft.com/en-us/library/0cbhe98f.aspx[^].

Use this property to find out the size: http://msdn.microsoft.com/en-us/library/system.drawing.image.size.aspx[^].

This is all you can do on server side.

At the same time, you can easily checkup the file size on client side using JavaScript:

image = new Image();
image.src = "myFile.png";
width = image.width;
height = image.height;



上传之前,您需要先进行此操作.

—SA



You need to do it before uploading.

—SA


这篇关于检查文件上传时图像的高度和宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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