if (this.FileUpload1.HasFile)//检查是否有文件
{
string fullFileName = this.FileUpload1.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.FileUpload1.PostedFile.ContentLength > 1024 * 1024)
{
Response.Write("<script>alert('上传图片太大!');</script>");
}
else
{
string path = HttpContext.Current.Request.MapPath("~/UpImg/");//获取上传文件的网站目录路径
this.FileUpload1.SaveAs(path + fileName);//存储文件到磁盘
}
}
else
{
Response.Write("<script>alert('非图片类型,不允许上传!');</script>");
}
}
else
{
Response.Write("<script>alert('必须指定文件!');</script>");
}