<% @ Import Namespace=" System.IO " %>
<html >
<BODY >
<SCRIPT LANGUAGE = "VB" RUNAT = "SERVER" >
Sub UploadFile_Clicked ( Sender as Object, e as EventArgs )
Dim lstrFileName as string
Dim lstrFileNamePath as string
Dim lstrFileFolder as string
' 如果上传目录为空,就使用" c:\ "作为缺省上传目录
' 获得上传到服务器的目录名称
if dir.value <> "" then
lstrFileFolder = dir.value
else
lstrFileFolder = "c:\"
end if
' 获得文件名称
lstrFileName = loFile.PostedFile.FileName
' 注: loFile.PostedFile.FileName 返回的是
通过文件对话框选择的文件名,这之中包含了文件的目录信息
lstrFileName = Path.GetFileName ( lstrFileName )
' 去掉目录信息,返回文件名称
' 判断上传目录是否存在,不存在就建立
If ( not Directory.Exists ( lstrFileFolder ) ) Then
Directory.CreateDirectory ( lstrFileFolder )
End If
'上传文件到服务器
lstrFileNamePath = lstrFileFolder & lstrFileName
' 得到上传目录及文件名称
loFile.PostedFile.SaveAs ( lstrFileNamePath )
' 获得并显示上传文件的属性
FileName.Text = lstrFileName
' 获得文件名称
FileType.Text = loFile.PostedFile.ContentType
' 获得文件类型
FileLength.Text = cStr ( loFile.PostedFile.ContentLength )
' 获得文件长度
FileUploadForm.visible = false
AnswerMsg.visible = true
' 显示上传文件属性
End sub
</SCRIPT >
<ASP:panel id = "FileUploadForm" visible = " true " runat = "server" >
<form method = "post" enctype = "multipart/form-data" runat = "server" >
<h1 > 欢迎使用ASP.NET页面实现文件上传 </h1 >
请选择上传到服务器的文件名称:
<input id = "loFile" type = "file" runat = "server" > <br >
请输入上传到服务器的目录名称:
<input id = "dir" type = "text" runat = "server" > <br >
<input type = "submit" value = "开始上传" OnServerClick = "
UploadFile_Clicked" runat = "server" >
<br >
</form >
</ASP:panel >
<ASP:panel id = "AnswerMsg" visible = " false " runat = "server" >
谢谢使用ASP.NET页面实现文件上传 <br >
成功上传 <ASP:label id = "FileName" runat = "server" /> <br >
文件大小 <ASP:label id = "FileLength" runat = "server" /> 字节 <br > 文件类型 <ASP:label id = "FileType " runat = "server" /> <br >
</ASP:panel >
</BODY >