把下面的代码保存为Upload.aspx即可运行(事先在同目录下建立一个Upload文件夹保存上传的文件,再建立一个数据库、表Upload,字段ID:自动编号,FilePath:文本型):
<
%@Import
Namespace =
Namespace
=
”
System.Data
”
%
>
<
%
‘
@Import Namespace=”System.Data.OleDb”%> <!–Access数据库用这个–>
<
%@Import
Namespace =
Namespace
=
”
System.Data.SqlClient
”
%
>
<
!
—
SQL Server数据库用这个
–>
<
script language
=
”
VB
”
runat
=
”
server
”
>
Sub UploadFile()
Sub
UploadFile(sender
As
Object
, e
As
EventArgs)
Dim
FileExt
FileExt
=
LCase
(
Right
(
Trim
(FileUp.Value),
3
))
If
FileExt
=
”
gif
”
Or
FileExt
=
”
jpg
”
Or
FileExt
=
”
bmp
”
Or
FileExt
=
”
png
”
Or
FileExt
=
”
tif
”
Or
LCase
(
Right
(
Trim
(FileUp.Value),
4
))
=
”
jpeg
”
Then
If
FileUp.PostedFile.ContentLength
=
0
Then
FileInfo.Visible
=
False
Exit Sub
Else
FileInfo.Visible
=
True
End
If
FSize.Text
=
CStr
(FileUp.PostedFile.ContentLength)
FName.Text
=
FileUp.PostedFile.FileName
Dim
FileSplit()
As
String
=
Split
( FileUp.PostedFile.FileName,
”
/
”
)
Dim
FileName
As
String
=
FileSplit(FileSplit.Length
–
1
)
FileUp.PostedFile.SaveAs( Server.MapPath(
”
.
”
)
&
”
/Upload/
”
&
FileName )
‘
把文件路径写入数据库 By Dicky 2005-7-12 9:26:29
‘
Access数据库用这个
‘
Dim objCommand As OleDbCommand
‘
Dim objConnection As OleDbConnection
‘
objConnection = New OleDbConnection(“Provider=Microsoft.Jet.OleDb.4.0;Data Source=”+Server.MapPath(“Upload.mdb”))
‘
objCommand = New OleDbCommand(“Insert Into Upload (FilePath) Values (‘Upload/”+FileName+”‘)” , objConnection)
‘
Access数据库用这个
‘
SQL Server数据库用这个
Dim
objCommand
As
SqlCommand
Dim
objConnection
As
SqlConnection
objConnection
=
New
SqlConnection(
”
Server=localhost;Uid=sa;Pwd=;Database=Shat_edg
”
)
objCommand
=
New
SqlCommand(
”
Insert Into Upload (FilePath) Values (‘Upload/
”
+
FileName
+
”
‘)
”
, objConnection)
‘
SQL Server数据库用这个
objConnection.Open()
objCommand.ExecuteNonQuery()
objConnection.Close()
‘
把文件路径写入数据库 By Dicky 2005-7-12 9:26:29
Dim
Exts()
As
String
=
Split
( FileName,
”
.
”
)
Dim
Ext
As
String
=
LCase
(Exts(Exts.Length
–
1
))
If
Ext
<>
”
jpg
”
And
Ext
<>
”
jpeg
”
And
Ext
<>
”
gif
”
And
Ext
<>
”
txt
”
And
Ext
<>
”
htm
”
And
Ext
<>
”
html
”
Then
FDisplay.Visible
=
False
Else
FDisplay.Text
=
”
<A Target=’_blank’ HREF=’Upload/
”
&
_
FileName
&
”
‘>上传文件</A>”
End
If
Response.
Write
(
”
上传成功!
”
)
Else
‘
Msgbox(“对不起,只能上传扩展名为gif、jpg、bmp、png、tif或jpeg等图片文件!”,65,”a”)
Response.
Write
(
”
对不起,只能上传扩展名为gif、jpg、bmp、png、tif或jpeg等图片文件!
”
)
End
If
End Sub
</
script
>
<
Html
>
<
head
>
<
title
>
文件上传
</
title
>
</
head
>
<
Body BgColor
=
White
>
<
H3
>
上传文件
<
Hr
></
H3
>
<
Form Name
=
”
Form1
”
Enctype
=
”
multipart/form-data
”
runat
=
”
server
”
>
上传文件
<
Input
Type
=
”
File
”
id
=
”
FileUp
”
runat
=
”
server
”
><
P
>
<
Asp:button id
=
”
Upload
”
OnClick
=
”
UploadFile
”
Text
=
”
Upload
”
runat
=
”
server
”
/>
</
form
><
Hr
>
<
Div id
=
”
FileInfo
”
Visible
=
”
False
”
runat
=
”
server
”
>
上传文件名
<
Asp:Label id
=
”
FName
”
runat
=
”
server
”
/><
br
>
上传文件大小
<
Asp:Label id
=
”
FSize
”
runat
=
”
server
”
/><
br
>
<
Asp:Label id
=
”
FDisplay
”
runat
=
”
server
”
/>
</
Div
>
</
Body
>
</
Html
>
版权声明:本文为AppleBBS原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。