ASP.NET图片上传的一段上传源码>>>>>>
代码一:
if (this.fudSc.HasFile )//检查是否有文件
{
string fullFileName = this.fudSc.PostedFile.FileName; //文件路径名
string fileName = fullFileName.Substring(fullFileName.LastIndexOf("//") + 1); //图片名称
string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1); //图片格式
if (type == "jpg" || type == "JPG" || type == "gif" || type == "GIF" || type == "BMP" || type == "bmp") //判断是否为图片类型
{
if (this.fudSc.PostedFile.ContentLength > 20 * 1024)
{
Response.Write("<script>alert('上传图片必须小于20k!');</script>");
}
else
{
string path = HttpContext.Current.Request.MapPath("~/zp/");//获取上传文件的网站目录路径
this.fudSc .SaveAs(path + fileName);//存储文件到磁盘
Response.Write("<script>alert('图片上传成功!');</script>");//提示
this.imgPicture .Src = "~/zp/" + fileName;//显示图片
}
}
else
{
Response.Write("<script>alert('非图片类型,不允许上传!');</script>");
}
}
else
{
Response.Write("<script>alert('必须指定文件!');</script>");
}