<% @ Page language="C#" %>
<% @ Import Namespace="System.IO" %>
![]()
< script runat ="server" >
void UploadButton_Click(object sender, EventArgs e)
{
// 指定在服務器端要保存上傳文件的路徑。
// *** 假定路徑存在***
string savePath = UploadPath.Text;
if (!Directory.Exists(savePath))
{
Response.Write(String.Format("<h1>上傳文件路徑不存在:{0}</h1>",
savePath));
Response.End();
}
// 在試圖操做文件前,
// 先驗證FileUpload 控件包含一個文件
if (FileUpload1.HasFile)
{
// 取得要上傳的文件名
string fileName = FileUpload1.FileName;
// 把文件名添加到上傳路徑
savePath += fileName;
// 調用SaveAs 方法來把上傳文件保存到指定路徑下。
// 注意,本示例並無實現全部必要錯誤檢查操做。
// 若是同名字的文件已經存在,則上傳文件將覆蓋之。
FileUpload1.SaveAs(savePath);
// 通知用戶文件名保存的位置
UploadStatusLabel.Text = "文件被保存爲: <i>" + savePath + "</i>";
}
else
{
// 通知用戶沒有上傳一個文件.
UploadStatusLabel.Text = "你沒有指定要上傳的文件。";
}
}
</ script >
< html >
< head runat ="server" >
< title > ASP.NET 2.0之HtmlInputFile控件使用小結 </ title >
</ head >
< body >
< form runat ="server" >
< h4 style ="text-align: center" >
< span style ="font-size: 24pt; color: #000099" > 請選擇要上傳的文件: </ span ></ h4 >
< b ></ b >< strong > 上傳文件保存路徑 </ strong >< br />
< asp:textbox id ="UploadPath" runat ="server" text ="c:\temp\" />
< hr />
< b > 要上傳的文件 </ b >< br />
< asp:fileupload id ="FileUpload1" runat ="server" />
< br >< br >
![]()
< asp:button id ="UploadButton"
text ="開始上傳文件"
onclick ="UploadButton_Click"
runat ="server" >
</ asp:button >
![]()
< hr />
![]()
< asp:label id ="UploadStatusLabel"
runat ="server" >
</ asp:label >
</ form >
</ body >
</ html >
![]()