国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > .NET > 正文

Asp.Net上傳文件示例(保存文件路徑到數(shù)據(jù)庫(kù))

2024-07-10 12:55:42
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

最大的網(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>
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 岚皋县| 绥德县| 威远县| 宁海县| 韶关市| 清苑县| 平和县| 麦盖提县| 项城市| 嘉峪关市| 呼伦贝尔市| 永福县| 武鸣县| 攀枝花市| 正宁县| 甘孜县| 昌邑市| 土默特左旗| 六安市| 哈巴河县| 建阳市| 巢湖市| 松阳县| 乡城县| 甘洛县| 富民县| 四会市| 昌宁县| 商丘市| 泗水县| 卓资县| 汉沽区| 五家渠市| 秀山| 洛阳市| 探索| 徐水县| 徐水县| 托克逊县| 兴城市| 营口市|