protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.PostedFile.FileName == "")
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('文件名称不能为空!');</script>");
return;
}
else
{
string filepath = FileUpload1.PostedFile.FileName;
Response.Write("文件全路径" + filepath);
string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
Response.Write("文件名称" + filename);
string fileex = filepath.Substring(filepath.LastIndexOf(".") + 1);
Response.Write("文件扩展名" + fileex);
string serverpath = Server.MapPath("img/") + filename;
Response.Write("文件存储全路径" + serverpath);
if (fileex == "png")
{
FileUpload1.PostedFile.SaveAs(serverpath);
Image1.ImageUrl = "img/" + filename;
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('上传成功!');</script>");
}
else
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('文件类型错误!');</script>");
}
}
}