<asp:Menu ID="NavigationMenu" runat="server" CssClass="menu"
EnableViewState="false" IncludeStyleBlock="false" Orientation="Horizontal"
>
<Items>
<asp:MenuItem NavigateUrl="~/Default.aspx" Text="上傳"/>
<asp:MenuItem NavigateUrl="~/Download.aspx" Text="下載"/>
</Items>
</asp:Menu>
</div>
</div>
<div class="main">
<asp:ContentPlaceHolder ID="MainContent" runat="server"/>
</div>
<div class="clear">
</div>
</div>
<div class="footer">
</div>
</form>
</body>
</html>
2、Default.aspx 上傳頁面
<%@ Page Title="文件上傳與下載" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="upAndDown._Default" %>
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
<h2>
上傳文件到服務器</h2>
<h2>
<asp:FileUpload ID="FileUpload1" runat="server" />
</h2>
<p>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="上傳" style="margin-left:170px;" />
</p>
</asp:Content>
3、Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace upAndDown
{
public partial class _Default : System.Web.UI.Page
{
PRotected void Page_Load(object sender, EventArgs e)
{
}
public Boolean UpLoad(string path, FileUpload fileupload)
{
bool fileOK = false;
if(fileupload.HasFile)
{
string fileException = System.IO.Path.GetExtension(fileupload.FileName).ToLower();
//上傳文件的格式
string[] allowedException = {".xls",".doc",".mp3",".rar",".zip",".vsd",".txt",".jpg",".gif",".bmp"
,".png",".swf",".avi",".mp3",".rm",".wma",".wmv",".exe"};
for (int i = 0; i < allowedException.Length;i++ )
{
if (fileException == allowedException[i])
fileOK = true; //返回成功標識
}
}
if (fileOK) //判斷上傳的文件是否在指定的格式范圍之內(nèi)
{
//判斷文件是否存在,如不存在則創(chuàng)建路徑
if (System.IO.Directory.Exists(path))
{
//該目錄存在,則將上傳的文件保存在該目錄當中
}
else
{
System.IO.Directory.CreateDirectory(path); //創(chuàng)建文件路徑
}
fileupload.SaveAs(path + "http://" + fileupload.FileName.Trim().Replace(" ","")); //去掉文件名字符空格,執(zhí)行文件上傳操作
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "name", "<script>alert('文件上傳成功!')</script>");
}
else
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert('hehh')", "<script>alert('不支持此格式文件上傳!')</script>");
}
return fileOK;
}
protected void Button1_Click(object sender, EventArgs e)
{
string FilePath = "";
//是否有上傳文件
if (this.FileUpload1.FileName != null && this.FileUpload1.FileName != "")
{
if (FileUpload1.PostedFile.ContentLength <= 0)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert('hehh')", "<script>alert('上傳文件為空文件,請重新選擇!')</script>");
return;
}
else
{
if (this.FileUpload1.HasFile)
{
if (FileUpload1.PostedFile.ContentLength > 524288000)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "scriptName", "<script>alert('上傳文件過大!')</script>");
return;
}
else
{
FilePath = Server.MapPath("/Images"); //設置服務器路徑
}
UpLoad(FilePath, this.FileUpload1);//調(diào)用UpLoad()函數(shù)上傳文件
}
}
}
//如果上傳文件錯誤,則返回操作
else
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "name", "<script>alert('文件為空,請先選擇要上傳的文件!')</script>");
return;
}
}
}
}