上传文件,比较完善
protected void btnfileup_Click(object sender, EventArgs e)
{
try
{
if (FileUpload1.PostedFile.FileName == "")
{
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('文件名称不能为空!');</script>");
return;
}
else
{
FileUpload1.PostedFile.SaveAs(Server.MapPath("file/" + FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\")+1)));
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('上传成功!');</script>");
}
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
Page.ClientScript.RegisterStartupScript(Page.GetType(), "message", "<script language='javascript' defer>alert('上传失败!');</script>");
}
}