ASP.NET上传图片,服务器端验证,宽度,高度,文件类型,尺寸,文件大小,图片上传,图片格式检查

首先在页面上的 form 里加入

//< INPUT style="WIDTH: 272px; HEIGHT: 22px" type="file" size="26" name="picfile" id="picfile" runat="server" >

代码里作如下定义

//protected System.Web.UI.HtmlControls.HtmlInputFile picfile;

//检查并上传

private bool CheckPic()
{
bool isSafe = false;
HttpPostedFile pic = picfile.PostedFile;
//扩展名检查
string picext = System.IO.Path.GetExtension(picfile.PostedFile.FileName).ToLower();
if(picext == ".jpg" || picext==".gif" || picext == "bmp" || picext =="png")
{
isSafe = true;
}
else
{
isSafe = false;
Kit.Alert(this.Page,"图片格式不对,请转换成常见的图片格式 jpg,gif,bmp,png");
return isSafe;
}
//图片大小检查
if(pic.ContentLength > PicMaxLength())
{
isSafe = false;
Kit.Alert(this.Page,"超过图片限制大小");
return isSafe;
}
//图片尺寸检查
System.IO.Stream picstream = pic.InputStream;
System.Drawing.Image img = System.Drawing.Image.FromStream(picstream);
if(img.Width > 0 && img.Height >0)
{
isSafe = true;
//第三步验证身份证图片并上传
SaveIdcard(txtidcard.Text.Trim());

picstream.Close();
picstream.Flush();
}
else
{
isSafe = false;
Kit.Alert(this.Page,"非法的图片文件");
//picstream.Close();
//picstream.Flush();
return isSafe;
}

return isSafe;
}

posted @ 2009-09-29 13:33  与时俱进  阅读(2276)  评论(0编辑  收藏  举报
友情链接:同里老宅院民居客栈