上传这个文件上传应该可以满足一般的需要了,其中的添加到数据库记录,您可以按需要省略掉。
代码:upload.aspx
<% Page Language="VB"%>
<% Import Namespace=" System.IO " %>
<% Import Namespace=" System.DATA " %>
<% import Namespace="System.Data.SqlClient" %>
<SCRIPT LANGUAGE = "VB" RUNAT = "SERVER">
Dim lstrFileFolder as string
Dim lstrFileFolder_1 as string
Dim lstrFileNamePath as string
Dim lstrFileNamePath_1 as string
Sub UploadFile_Clicked ( Sender as Object, e as EventArgs )
Dim lstrFileName as string
'Dim lstrFileFolder as string
Dim CMDfilename as string '表单cmdfilename的值
CMDfilename = request("CMDfilename")
'''
'''
' 如果上传目录为空,就使用" upload "作为缺省上传目录
' 获得上传到服务器的目录名称
if dir.value <> "" then
lstrFileFolder_1 = dir.value & "/"
lstrFileFolder = trim((Server.MapPath(""))) & "\" & dir.value & "\"
else
lstrFileFolder_1 = "upload/"
lstrFileFolder = trim((Server.MapPath(""))) &"\upload\"
end if
' 获得文件名称
lstrFileName = loFile.PostedFile.FileName
' 注: loFile.PostedFile.FileName 返回的是
'通过文件对话框选择的文件名,这之中包含了文件的目录信息
'只能上传.xls文件
if instr(Path.GetFileName ( lstrFileName ),".xls")=0 and instr(Path.GetFileName ( lstrFileName ),".Xls")=0 and instr(Path.GetFileName ( lstrFileName ),".XLS")=0 then
MessageNotXLS.InnerHtml = "只能上传.xls文件"
MessageNotXLS.Style("color") = "red"
exit sub
end if
if CMDfilename = "random" then
dim lstrFileName111 as string = Path.GetFileName ( lstrFileName ) '获得原名
lstrFileName=now()
lstrFileName=replace(lstrFileName,"-","")
lstrFileName=replace(lstrFileName,":","")
lstrFileName=replace(lstrFileName," ","")
'lstrFileName=lstrFileName+"."
lstrFileName=lstrFileName & right(Path.GetFileName ( lstrFileName111 ),len(Path.GetFileName ( lstrFileName111 ))-(instr(Path.GetFileName ( lstrFileName111 ),"."))+1)
else
lstrFileName = Path.GetFileName ( lstrFileName )
end if
' 去掉目录信息,返回文件名称
' 判断上传目录是否存在,不存在就建立
If ( not Directory.Exists ( lstrFileFolder ) ) Then
Directory.CreateDirectory ( lstrFileFolder )
End If
'上传文件到服务器
lstrFileNamePath = lstrFileFolder & lstrFileName
lstrFileNamePath_1 = lstrFileFolder_1 & 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
' 显示上传文件属性
AddToDB()
End sub
'添加记录信息到数据库,可以不要这步。
sub AddToDB()
Dim MyConnection As SQLConnection = New SQLConnection (ConfigurationSettings.AppSettings("ConnStr"))
Dim InsertCmd As String = "insert into chaoqi (title,filepath) values (title,filepath)"
Dim MyCommand As SqlCommand = new SqlCommand(InsertCmd, MyConnection)
MyCommand.Parameters.Add(New SqlParameter("title", SqlDbType.NVarChar, 50))
MyCommand.Parameters("title").Value = title.value
MyCommand.Parameters.Add(New SqlParameter("filepath", SqlDbType.NVarChar, 50))
MyCommand.Parameters("filepath").Value = lstrFileNamePath_1
MyCommand.Connection.Open()
Try
MyCommand.ExecuteNonQuery()
Message.InnerHtml = "<b>已添加记录</b>
" & InsertCmd.ToString()
Catch Exp As SQLException
If Exp.Number = 2627
Message.InnerHtml = "错误:已存在具有相同主键的记录"
Else
Message.InnerHtml = "错误:未能添加记录,请确保正确填写了字段"
End If
Message.Style("color") = "red"
End Try
MyCommand.Connection.Close()
end sub
</SCRIPT>
<html>
<head>
<title>上传文件</title>
<link language="JavaScript">
function OnToggleTOCLevel1()
{
var elemSrc = window.event.srcElement;
var elemLevel2 = document.all(elemSrc.level2ID);
if (elemLevel2.style.display == 'none')
{
elemLevel2.style.display = '';
if (elemSrc.usesGlyph == '1')
elemSrc.innerHTML = '6';
}
else {
elemLevel2.style.display = 'none';
if (elemSrc.usesGlyph == '1')
elemSrc.innerHTML = '4';
}
}
</script>
</BODY>
</HTML>
一个加强的文件上传(VB.Net)
80酷酷网 80kuku.com