首先在页面上的 form 里加入
//< INPUT style="WIDTH: 272px; HEIGHT: 22px" type="file" size="26" name="picfile" >
代码里作如下定义
//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;
}
|
请发表评论