Username: Password:

Asp.Net上传文档示例(保存文档路径到数据库)-.NET教程,Asp.Net研发
来源:作者: 发布时间:2007-12-25 13:37:29

 

把下面的代码保存为upload.aspx即可运行(事先在同目录下建立一个upload文档夹保存上传的文档,再建立一个数据库、表upload,字段id:自动编号,filepath:文本型):

<%@import namespace="system.data"%>
<%@import namespace="system.data.oledb"%> 
<%@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 
= "" & _
        filename 
& ">上传文档"
        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>

喜欢本文,那就收藏到:

    Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网
相关评论  我也要评论
还没有关于此文章的相关评论!
  • 昵称: (为空则显示guest)
  • 评论分数: ★ ★ ★★★ ★★★★ ★★★★★
  • 评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
  • 导航
    赞助商
    文章类别
    订阅