asp.net 图片上传与显示

代码
protected void Button1_Click(object sender, EventArgs e)
    {
        
string fileName = this.File1.PostedFile.FileName;   //取得客户端文件名

        
string type = fileName.Substring(fileName.LastIndexOf(".")+1).ToLower();    //取得文件的后缀名

        
if (type == "jpg" || type=="gif" || type=="jpeg" )     //判断文件格式
        {
            
string savePath = Server.MapPath("~/upload/");      //指定上传文件在服务器上的保存路径

            
if (!Directory.Exists(savePath))      //检查服务器上是否存在这个物理路径
            {
                Directory.CreateDirectory(savePath); 
//如果不存在则创建 
            }

            
this.File1.PostedFile.SaveAs(savePath + "\\" + fileName); //上传

             
this.Image1.ImageUrl = "Image/" + filename;//上传成功以后显示该图片
            
        }
        
else
        {
             Response.Write(
"<Script language='javascript'> alert('格式错误')</Script>");
        }

        
    }
原文地址:https://www.cnblogs.com/fanxianhua/p/1650366.html