最大的網(wǎng)站源碼資源下載站,
把下面的代碼保存為upload.aspx即可運(yùn)行(事先在同目錄下建立一個(gè)upload文件夾保存上傳的文件,再建立一個(gè)數(shù)據(jù)庫(kù)、表upload,字段id:自動(dòng)編號(hào),filepath:文本型):

<%@import namespace =namespace="system.data"%>
<%'@import namespace="system.data.oledb"%> <!--access數(shù)據(jù)庫(kù)用這個(gè)-->
<%@import namespace =namespace="system.data.sqlclient"%> <!--sql server數(shù)據(jù)庫(kù)用這個(gè)-->
<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 )
'把文件路徑寫入數(shù)據(jù)庫(kù) by dicky 2005-7-12 9:26:29
' access數(shù)據(jù)庫(kù)用這個(gè)
' 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數(shù)據(jù)庫(kù)用這個(gè)
' sql server數(shù)據(jù)庫(kù)用這個(gè)
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數(shù)據(jù)庫(kù)用這個(gè)
objconnection.open()
objcommand.executenonquery()
objconnection.close()
'把文件路徑寫入數(shù)據(jù)庫(kù) 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("對(duì)不起,只能上傳擴(kuò)展名為gif、jpg、bmp、png、tif或jpeg等圖片文件!",65,"a")
response.write("對(duì)不起,只能上傳擴(kuò)展名為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>新聞熱點(diǎn)
疑難解答
圖片精選