图片上传

图片上传

//设置图片的格式

protected bool   IsAllowedExtension(FileUpload file)

{

   System.OI.FileInfo info=new System.IO.fileInfo(file.fileName);

   Return file.Extension.ToUpper()==@".JPG"|| file.Extension.ToUpper()==@".GIF";  

}

//fileUpload控件有上传文件时

if(FileUpload1.HasFile)

{

//如果图片格式true

if(IsAllowedExtension(FileUpload1))

{

string filename=FileUpload1.FileName;   //FileUpload1:FilUpload控件,获取图片名称

string Path=Server.MapPath(路径 +@"");  //获取绝对路径

string savePath=Path+filename;     //拼接路径

//如果路径不存在,创建文件路径

//if path not exists create path

if(!Directory.Exists(path))

{

  Directory.CreateDirectory(path);

}

//save photo to savePath

//保存上传的图片到指定的路径

FileUpload1.SaveAs(savePath);

}

}

原文地址:https://www.cnblogs.com/huangchenqin/p/5520157.html