学ASP.NET第一次完全自己的代码,那种成就感实在无法可以形容的
wow~~

<%@ Import Namespace =Namespace="System.IO" %>

<%@ Import Namespace =Namespace="System.Data.OleDb" %>

<%@ Import Namespace =Namespace="System.Web.HttpUtility" %>

<%@ Import Namespace =Namespace="System.Web.HttpCookie" %>
<html>
<script language="VB" runat="server" >


Sub Page_Load()Sub Page_Load(Sender as Object, E as EventArgs)
btnOK.Attributes("onclick") = "return check()"
end sub


Sub upFile_Click()Sub upFile_Click(Sender as Object, E as EventArgs)
dim connStr as string,Conn As OleDbConnection, strSQL As String, ds As OleDbDataReader, comm as New OleDbCommand
dim fName as string,fext as string,fsize as integer, hasExt as boolean
dim savePath as string,treeid as integer,pid as integer
dim title,desc,uper as string
try
fname=UploadFile.PostedFile.FileName
fname=Right(fName, fName.Length - fName.LastIndexOf("") - 1)
fext=Right(fName, fName.Length - fName.LastIndexOf(".") - 1)
fsize=UploadFile.PostedFile.ContentLength
'//检查所上传文件的大小------------------------------------------------------------------- if fsize>1000000 or fsize=0 then
'上传大小限制
response.write("<p align=center><font color='#FF0000'>文件大小超出限制~</font></p>")
exit sub
end if
'//读取数据库---------------------------------------------------------------------
connStr="PROVIDER=MICROSOFT.JET.OLEDB.4.0;DATA SOURCE='" & Server.mappath("db.mdb") & "'"
strSql="select * from queryType where extName='"& ucase(fext) &"'"
Conn=New OleDbConnection(connStr)
conn.open()
comm.commandText=strSQL
comm.Connection=conn
comm.CommandType =1
ds=comm.ExecuteReader()
while ds.read()
savePath =ds("savePath")
treeid=ds("treeID")
pid=ds("id")
hasExt=true
end while
ds.close()
'替换HTML字符

title=HtmlEncode(ntitle.text)
desc=replace(descript.text,vbCrLf," ")
desc=HtmlEncode(desc)
uper=HtmlEncode(uploader.text)
catch
if conn.state<>0 then
conn.close()
end if
Response.Write("<p align=center> <font color='#FF0000'>出错了~," & Err.Description & Err.Source & "</font></p>")
exit sub
end try
if hasExt then
try
'插入到数据库中
strSql="insert into filelist(caption,tmemo,u_name,upTime,fileType,parentID,fileSize) values("
strSql=strSql & "'" & title & "','" & desc & "','" & uper & "','" & now() & "'," & pid.toString() & "," & treeid.toString() & "," & fsize.toString() & ")"
comm.commandText=strSql
comm.ExecuteNonQuery()
'从中取出新ID作为文件名
comm.commandText="Select @@identity AS newId"
ds=comm.ExecuteReader()
dim newId as integer
while ds.read()
newid=ds(0)
end while
ds.close()
dim newFileName as string =replace(fname,"." & fext ,"") & "_" & newid.toString() & "." & fext
'更新数据库
comm.commandText="update filelist set filePath='" & savePath & newFileName & "' where id=" & newid.toString()
comm.ExecuteNonQuery()
UploadFile.PostedFile.SaveAs(Server.MapPath(savePath & newFileName ))
ntitle.text=""
response.write("<script language='javascript'> alert('上传文件成功~~')</" & "script>")
catch
if conn.state<>0 then
conn.close()
end if
Response.Write("<p align=center> <font color='#FF0000'>出错了~," & Err.Description & Err.Source & "</font></p>")
exit sub
end try
else
response.write("<p align=center> <font color='#FF0000'>不能上传" & fext &"类型文件~</font></p>")
end if
if conn.state<>0 then
conn.close()
end if

End Sub

</script>
<head>

<script language="JavaScript">

Function check()function check(){
if (document.all.ntitle.value.length>25||document.all.ntitle.value.length==0){
alert("标题文字的长度不能为0或者大于25")
document.all.ntitle.focus()
return false
}
if (document.all.descript.value.length>80||document.all.descript.value.length==0){
alert("描述文字的长度不能为0或者大于80")
document.all.descript.focus()
return false
}
if (document.all.uploader.value.length>20||document.all.uploader.value.length==0){
alert("上传人名长度不能为0或者大于20")
document.all.uploader.focus()
return false
}
if (document.all.UploadFile.value.length==0){
alert("请选择一个文件后选择上传")
document.all.uploader.focus()
return false
}

return true
}
</script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head>
<body bgcolor="#FFFFF5" background="image/bg.gif">
<br>
<form enctype="multipart/form-data" runat=server>
<table width="520" height="335" border="0" align="center" cellpadding="6" cellspacing="1"
bgcolor="#666666">
<tr align="center" bgcolor="#ccccc5">
<td height="22" colspan="2"><strong>上传限制为1M的文件,只允许上传mp3,mid,jpg,gif,zip,rar,seem,sch,bin格式文件</strong></td>
</tr>
<tr bgcolor="#ddddd5">
<td width="120" height="29" align="right">
标题:</td>
<td width="373">
<asp:TextBox id="ntitle" runat="server" Width="304px" ></asp:TextBox></td>
</tr>
<tr bgcolor="#ddddd5">
<td height="159" align="right" valign="middle" bgcolor="#ddddd5">
描述:</td>
<td> <asp:textbox ID="descript" runat="server" Width="304px" Height="146px" TextMode="MultiLine"></asp:textbox></td>
</tr>
<tr bgcolor="#ddddd5">
<td height="31" align="right">
上传人:</td>
<td>
<asp:TextBox id="uploader" runat="server" Width="304px"></asp:TextBox></td>
</tr>
<tr bgcolor="#ddddd5">
<td height="31" align="right">
文件:</td>
<td>
<input type="file" id="UploadFile" runat="server" style="WIDTH:300px;HEIGHT:20px"></td>
</tr>
<tr bgcolor="#ddddd5">
<td colspan="2" align="center" style="HEIGHT: 19px">
<asp:Button id="btnOk" runat="server" Text="上传" onClick="upFile_Click" Width="105px" Height="24px"></asp:Button>
</td>
</tr>
<tr bgcolor="#ccccc5">
<td colspan="2" align="center"><font color="#CC0000"><strong><font color="#CC0000"><script language="JavaScript">ol(10)</script></font>
</strong> </font></td>
</tr>
</table>


</form>
</body>
</html>
