上传图片
来源:互联网 发布:傅园慧走红网络 编辑:程序博客网 时间:2024/04/30 02:15
protected void Page_Load(object sender, EventArgs e)
{
ViewState["filePath"] = "";
//Session["imagePath"];
}
/// <summary>
/// 上传
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnsc_Click(object sender, EventArgs e)
{
if (this.fileupload1.HasFile)
{
if (ViewState["filePath"].ToString() != "")
{
{
ViewState["filePath"] = "";
//Session["imagePath"];
}
/// <summary>
/// 上传
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void btnsc_Click(object sender, EventArgs e)
{
if (this.fileupload1.HasFile)
{
if (ViewState["filePath"].ToString() != "")
{
SQLHelper.FileManager.DeleteExistsImage(Server.MapPath(ViewState ["filePath"].ToString()));
}
}
FileInfo file = new FileInfo(this.fileupload1.FileName);
string _file = file.Extension.ToLower().ToString();
if (_file.ToString() == ".gif" || _file.ToString() == ".jpg" || _file.ToString() == ".bmp" || _file.ToString() == ".png")
{
//上传保存的时候更改名称
//ViewState["filePath"] = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + his.FileUpload1.FileName.ToString();
ViewState["filePath"] = "~/UpFile" + "//" + DateTime.Now.ToString ("yyyyMMddHHmmss") + _file;
this.fileupload1.SaveAs(Server.MapPath(ViewState["filePath"].ToString()));
//this.Image1.Text = ViewState["filePath"].ToString();
this.Image1.ImageUrl = ViewState["filePath"].ToString();
Response.Write("<Script Language = Javascript>alert('您的图片上传成 功!');</Script>");
}
else
{
Response.Write("<Script Langauge = Javascript>alert('您的图片格式上传错 误!');</Script>");
}
}
string _file = file.Extension.ToLower().ToString();
if (_file.ToString() == ".gif" || _file.ToString() == ".jpg" || _file.ToString() == ".bmp" || _file.ToString() == ".png")
{
//上传保存的时候更改名称
//ViewState["filePath"] = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + his.FileUpload1.FileName.ToString();
ViewState["filePath"] = "~/UpFile" + "//" + DateTime.Now.ToString ("yyyyMMddHHmmss") + _file;
this.fileupload1.SaveAs(Server.MapPath(ViewState["filePath"].ToString()));
//this.Image1.Text = ViewState["filePath"].ToString();
this.Image1.ImageUrl = ViewState["filePath"].ToString();
Response.Write("<Script Language = Javascript>alert('您的图片上传成 功!');</Script>");
}
else
{
Response.Write("<Script Langauge = Javascript>alert('您的图片格式上传错 误!');</Script>");
}
}
//if (this.fileupload1.FileName.ToString() == "")
//{
// Response.Write("<script>alert('您还没有选择图片!');</script>");
//}
//else
//{
// Session["imagePath"] = "~/UpFile" + this.fileupload1.FileName.ToString();
// fileupload1.SaveAs(Server.MapPath(Session["imagePath"].ToString()));
// Image1.ImageUrl = Session["imagePath"].ToString();
//}
}
//{
// Response.Write("<script>alert('您还没有选择图片!');</script>");
//}
//else
//{
// Session["imagePath"] = "~/UpFile" + this.fileupload1.FileName.ToString();
// fileupload1.SaveAs(Server.MapPath(Session["imagePath"].ToString()));
// Image1.ImageUrl = Session["imagePath"].ToString();
//}
}
public class FileManager
{
/// <summary>
/// 删除已存在的图片
/// </summary>
/// <param name="path"></param>
public static void DeleteExistsImage(string path)
{
try
{
FileInfo file = new FileInfo(path);
if (file.Exists)
{
file.Delete();
}
}
catch(IOException ex)
{
throw ex;
}
}
}