把下面的代码保存为upload.aspx即可运行(事先在同目录下建立一个upload文件夹保存上传的文件,再建立一个数据库、表upload,字段id:自动编号,filepath:文本型):
<%@import namespace=“system.data“%>
<%@import namespace=”system.data.oledb”%> <!–access数据库用这个–>
<%@import namespace=“system.data.sqlclient“%> <!—sql server数据库用这个–>
<script language=“vb“ runat=“server“>
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>
<%@import namespace=”system.data.oledb”%> <!–access数据库用这个–>
<%@import namespace=“system.data.sqlclient“%> <!—sql server数据库用这个–>
<script language=“vb“ runat=“server“>
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>