<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" /></div>
</form>
</body>
</html>
后台代码:Default.aspx.cs<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" /></div>
</form>
</body>
</html>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string FileName = FileUpload1.FileName;
//取得文件类型
string strFileExtend = FileName.Substring(FileName.LastIndexOf(".")+1);
string[] NoFileExtend ={ "exe","php","asp"};
bool Flag = true;
for (int i = 0; i < NoFileExtend.Length; i++)
{
if (strFileExtend.Equals(NoFileExtend[i]))
{
Flag = false;
}
}
if (Flag)
{
string strFilePath = string.Format("files/{0}.{1}", DateTime.Now.ToString("mmhhddss"), strFileExtend);
string path = Server.MapPath(strFilePath);
FileUpload1.SaveAs(path);
Page.ClientScript.RegisterStartupScript(this.GetType(), "Startup", "<script>alert('文件上传成功');</script>");
Response.Write("格式化后的地址为:"+strFilePath+"文件类型为:"+strFileExtend);
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Startup", "<script>alert('" + string.Format("不能上传{0}格式文件", strFileExtend) + "');</script>");
}
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"Startup","<script>alert('文件不能为空');</script>");
}
}
}
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string FileName = FileUpload1.FileName;
//取得文件类型
string strFileExtend = FileName.Substring(FileName.LastIndexOf(".")+1);
string[] NoFileExtend ={ "exe","php","asp"};
bool Flag = true;
for (int i = 0; i < NoFileExtend.Length; i++)
{
if (strFileExtend.Equals(NoFileExtend[i]))
{
Flag = false;
}
}
if (Flag)
{
string strFilePath = string.Format("files/{0}.{1}", DateTime.Now.ToString("mmhhddss"), strFileExtend);
string path = Server.MapPath(strFilePath);
FileUpload1.SaveAs(path);
Page.ClientScript.RegisterStartupScript(this.GetType(), "Startup", "<script>alert('文件上传成功');</script>");
Response.Write("格式化后的地址为:"+strFilePath+"文件类型为:"+strFileExtend);
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Startup", "<script>alert('" + string.Format("不能上传{0}格式文件", strFileExtend) + "');</script>");
}
}
else
{
Page.ClientScript.RegisterStartupScript(this.GetType(),"Startup","<script>alert('文件不能为空');</script>");
}
}
}