相關(guān)資訊
本類常用軟件
-
福建農(nóng)村信用社手機(jī)銀行客戶端下載下載量:584204
-
Windows優(yōu)化大師下載量:416896
-
90美女秀(視頻聊天軟件)下載量:366961
-
廣西農(nóng)村信用社手機(jī)銀行客戶端下載下載量:365699
-
快播手機(jī)版下載量:325855
本文分享給大家的是關(guān)于ASP上傳功能的實(shí)例詳解,希望對正在學(xué)習(xí)ASP的朋友們能夠有幫助。
這個(gè)問題已經(jīng)不是什么新鮮問題了,網(wǎng)上也有大把的教程,但大多數(shù)是授人以魚,而不授人以漁,經(jīng)過辛苦的資料收集,思考,調(diào)試,整理,我基本上已經(jīng)把這個(gè)問題從原理上搞清楚了,現(xiàn)在根據(jù)我自己的理解,在范例程序的基礎(chǔ)上,加以解釋,希望能對部分網(wǎng)友有所幫助。
請諸位大蝦能對其中的不正或不良這處予以指正。
我想循序漸進(jìn),先講一個(gè)簡單的,單個(gè)圖片文件保存到數(shù)據(jù)庫。
這個(gè)范例共包括三個(gè)ASP文件和一個(gè)數(shù)據(jù)庫(一個(gè)表),全部在同一目錄下。
。、tblImage 表結(jié)構(gòu)(access 2000)
sn 自動(dòng)編號 序列號
content-type 文本 圖片類型
image OLE 對象 圖片數(shù)據(jù)
2、SimpleImageToData.asp:上傳表單及保存圖片到數(shù)據(jù)庫的代碼部分,主要文件。
<%@ Language=VBScript %>
<% option explicit %>
<%
'從一個(gè)完整路徑中析出文件名稱
function getFileNamefromPath(strPath)
getFileNamefromPath = mid(strPath,instrrev(strPath,"\")+1)
end function
'定義數(shù)據(jù)庫連接字符串
dim cnstr
cnstr = "driver={Microsoft Access Driver (*.mdb)};dbq=" & server.MapPath("./upload.mdb")
%>
<HTML>
<HEAD>
<title>單個(gè)圖像保存到數(shù)據(jù)庫</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<body>
<p><a href="SimpleImageToData.asp">上傳圖片</a>
<a href="ShowImageListFromData.asp">顯示圖片</a><hr></p>
<%
if request.ServerVariables("REQUEST_METHOD") = "POST" then
dim sCome, sGo, binData, strData
dim posB, posE, posSB, posSE
dim binCrlf
dim strPath, strFileName, strContentType
binCrlf = chrb(13)&chrb(10) '定義一個(gè)單字節(jié)的回車換行符
set sCome = server.CreateObject("adodb.stream")
sCome.Type = 1 '指定返回?cái)?shù)據(jù)類型 adTypeBinary=1,adTypeText=2
sCome.Mode = 3 '指定打開模式 adModeRead=1,adModeWrite=2,adModeReadWrite=3
sCome.Open
sCome.Write request.BinaryRead(request.TotalBytes)
sCome.Position = 0
binData = sCome.Read
'response.BinaryWrite binData '調(diào)試用:顯示提交的所有數(shù)據(jù)
'response.Write "<hr>" '調(diào)試用
set sGo = server.CreateObject("adodb.stream")
sGo.Type = 1
sGo.Mode = 3
sGo.Open
posB = 1
posB = instrb(posB,binData,binCrlf)
posE = instrb(posB+1,binData,binCrlf)
'response.Write posB & " | " & posE & "<br>"
sCome.Position = posB+1
sCome.CopyTo sGo,posE-posB-2
sGo.Position = 0
sGo.Type = 2
sGo.Charset = "gb2312"
strData = sGo.ReadText
sGo.Close
'response.Write strData & "<hr>"
posSB = 1
posSB = instr(posSB,strData,"filename=""") + len("filename=""")
posSE = instr(posSB,strData,"""")
if posSE > posSB then
strPath = mid(strData,posSB,posSE-posSB)
'response.Write "本地路徑:" & strPath & "<br>"
'response.Write "文件名:" & getFileNamefromPath(strPath) & "<br>"
posB = posE
posE = instrb(posB+1,binData,binCrlf)
'response.Write posB & " | " & posE & "<br>"
sGo.Type = 1
sGo.Mode = 3
sGo.Open
sCome.Position = posB
sCome.CopyTo sGo,posE-posB-1
sGo.Position = 0
sGo.Type = 2
sGo.Charset = "gb2312"
strData = sGo.ReadText
sGo.Close
strContentType = mid(strData,16) '此處因?yàn)楣潭ǖ,所以省略查?:-)
'response.Write "圖片類型:" & strContentType & "<hr>"
posB = posE+2
posE = instrb(posB+1,binData,binCrlf)
'response.Write posB & " | " & posE & "<br>"
sGo.Type = 1
sGo.Mode = 3
sGo.Open
sCome.Position = posB+1
sCome.CopyTo sGo,posE-posB-2
sGo.Position = 0
strData = sGo.Read
sGo.Close
'response.Write lenb(strData) & "<br>"
dim cn, rs, sql
set cn = server.CreateObject("adodb.connection")
cn.Open cnstr
set rs = server.CreateObject("adodb.recordset")
sql = "select * from tblImage"
rs.Open sql,cn,1,3
rs.AddNew
rs.Fields("content-type").Value = strContentType
rs.Fields("image").AppendChunk strData
rs.Update
rs.Close
set rs = nothing
cn.Close
set cn = nothing
response.Write "圖片保存成功!" & "<br>"
else
response.Write "沒有上傳圖片!" & "<br>"
end if
set sGo = nothing
sCome.Close
set sCome = nothing
else
%>
<form id="frmUpload" name="frmUpload" action="SimpleImageToData.asp" method="post" target="_self" enctype="multipart/form-data">
<INPUT id="filImage" type="file" name="filImage" size="40">
<BR>
<INPUT id="btnUpload" type="submit" value="Upload" name="btnUpload">
</form>
<%
end if
%>
</body>
</HTML>
。、ShowImageListFromData.asp
<%@ Language=VBScript %>
<% option explicit %>
<html>
<head>
<title>顯示數(shù)據(jù)庫中已有圖片的列表</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<p><a href="SimpleImageToData.asp">上傳圖片</a>
<a href="ShowImageListFromData.asp">顯示圖片</a><hr></p>
<table border=0 cellpadding=2 cellspacing=2>
<tr>
<td valign=top>
<%
dim cnstr
cnstr = "driver={Microsoft Access Driver (*.mdb)};dbq=" & server.MapPath("./upload.mdb")
dim cn, sql, rs
set cn = server.CreateObject("adodb.connection")
cn.Open cnstr
sql = "select sn,[content-type],image from tblImage"
set rs = cn.Execute(sql)
response.Write "<table border=1 cellspacing=2 cellpadding=5>"
response.Write "<tr>"
response.Write "<th>序列號</th><th>圖片類型</th><th>圖片</th>"
response.Write "</tr>"
do until rs.eof
response.Write "<tr>"
response.Write "<td>" & rs("sn") & "</td>"
response.Write "<td>" & rs("content-type") & "</td>"
response.Write "<td><a href='ShowImageListFromData.asp?sn=" & rs("sn") & "'>看圖</a></td>"
response.Write "</tr>"
rs.movenext
loop
response.Write "</table>"
cn.Close
set cn = nothing
%>
</td>
<td valign=top>
<%
dim sn
sn = request.QueryString("sn")
if sn = "" then
response.Write "沒有指定圖片!"
else
response.Write "<img border=1 src=ShowImageFromData.asp?sn=" & sn & ">"
end if
%>
</td>
</tr>
</table>
</body>
</html>
。、ShowImageFromData.asp
<%@ Language=VBScript %>
<% option explicit %>
<%
dim sn
sn = request.QueryString("sn")
if sn = "" then response.End
dim cnstr
cnstr = "driver={Microsoft Access Driver (*.mdb)};dbq=" & server.MapPath("./upload.mdb")
dim cn, sql, rs
set cn = server.CreateObject("adodb.connection")
cn.Open cnstr
sql = "select sn,[content-type],image from tblImage where sn=" & cint(sn)
set rs = cn.Execute(sql)
response.ContentType = rs("content-type")
response.BinaryWrite rs("image")
set rs = nothing
cn.Close
set cn = nothing
%>
上面講了單個(gè)圖片文件保存到數(shù)據(jù)庫,下面講一下文本信息與圖片文件同時(shí)提交保存到數(shù)據(jù)庫,圖片文件也可保存到磁盤文件。
MultiInputOrImageToData.asp
<%@ Language=VBScript %>
<% option explicit %>
<%
'把一段二進(jìn)制數(shù)據(jù)寫入到一個(gè)文件
sub saveBin2File(srmSource,posB,posLen,strPath)
dim srmObj
set srmObj = server.CreateObject("adodb.stream")
srmObj.Type = 1
srmObj.Mode = 3
srmObj.Open
srmSource.Position = posB-1
srmSource.CopyTo srmObj,posLen
srmObj.Position = 0
srmObj.SaveToFile strPath,2 '如果該文件已經(jīng)存在,無條件覆蓋
srmObj.Close
set srmObj = nothing
end sub
'二進(jìn)制數(shù)據(jù)轉(zhuǎn)換為字符串,包括漢字
function getTextfromBin(srmSource,posBegin,posLen)
dim srmObj, strData
set srmObj = server.CreateObject("adodb.stream")
srmObj.Type = 1
srmObj.Mode = 3
srmObj.Open
srmSource.position = posBegin-1 '位置計(jì)數(shù)首數(shù)不一樣,這個(gè)對象是對0開始的
srmSource.CopyTo srmObj,posLen
srmObj.Position = 0
srmObj.Type = 2
srmObj.Charset = "gb2312"
strData = srmObj.ReadText
srmObj.Close
set srmObj = nothing
getTextfromBin = strData
end function
'雙字節(jié)字符串轉(zhuǎn)換成單字節(jié)字符串
function getSBfromDB(bytString)
dim bin, i
bin = ""
for i=1 to len(bytString)
bin = bin & chrb(asc(mid(bytString,i,1)))
next
getSBfromDB = bin
end function
'單字節(jié)字符串轉(zhuǎn)換成雙字節(jié)字符串
function getDBfromSB(bitString)
dim str, i
str = ""
for i=1 to lenb(bitString)
str = str & chr(ascb(midb(bitString,i,1)))
next
getDBfromSB = str
end function
'從一個(gè)完整路徑中析出文件名稱
function getFileNamefromPath(strPath)
getFileNamefromPath = mid(strPath,instrrev(strPath,"\")+1)
end function
'判斷函數(shù)
function iif(cond,expr1,expr2)
if cond then
iif = expr1
else
iif = expr2
end if
end function
'定義數(shù)據(jù)庫連接字符串
dim cnstr
cnstr = "driver={Microsoft Access Driver (*.mdb)};dbq=" & server.MapPath("./upload.mdb")
%>
<HTML>
<HEAD>
<title>多個(gè)表單域或圖像同步保存到數(shù)據(jù)庫</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<body>
<p>導(dǎo)航菜單:<b>上傳圖片</b> <a href="ShowImageListFromData2.asp">顯示圖片</a><hr></p>
<%
if request.ServerVariables("REQUEST_METHOD") = "POST" then
dim sCome, binData
dim posB, posE, posSB, posSE
dim binCrlf, binSub
dim strTitle, strFileName, strContentType, posFileBegin, posFileLen, aryFileInfo
dim i, j
dim dicData
dim strName,strValue
binCrlf = getSBfromDB(vbcrlf) '定義一個(gè)單字節(jié)的回車換行符
binSub = getSBfromDB("--") '定義一個(gè)單字節(jié)的“--”字符串
set sCome = server.CreateObject("adodb.stream")
sCome.Type = 1 '指定返回?cái)?shù)據(jù)類型 adTypeBinary=1,adTypeText=2
sCome.Mode = 3 '指定打開模式 adModeRead=1,adModeWrite=2,adModeReadWrite=3
sCome.Open
sCome.Write request.BinaryRead(request.TotalBytes)
sCome.Position = 0
binData = sCome.Read
'response.BinaryWrite binData '調(diào)試用:顯示提交的所有數(shù)據(jù)
'response.Write "<hr>" '調(diào)試用
posB = instrb(binData,binSub)
posB = instrb(posB,binData,bincrlf) + 2 '+2是加入回車換行符本身的長度
posB = instrb(posB,binData,getSBfromDB("name=""")) + 6
set dicData = server.CreateObject("scripting.dictionary") '用來保存信息
do until posB=6
posE = instrb(posB,binData,getSBfromDB(""""))
'Response.Write "name=" & getTextfromBin(sCome,posB,posE-posB) & "<br>"
strName = getTextfromBin(sCome,posB,posE-posB)
posB = posE + 1 '指針移動(dòng)到“"”的后面
posE = instrb(posB,binData,bincrlf)
'Response.Write posB & "->" & posE & "<br>"
if instrb(midb(binData,posB,posE-posB),getSBfromDB("filename=""")) > 0 then '判斷成功表示這是一個(gè)file域
posB = instrb(posB,binData,getSBfromDB("filename=""")) + 10
posE = instrb(posB,binData,getSBfromDB(""""))
if posE>posB then
'response.Write "filename=" & getTextfromBin(sCome,posB,posE-posB) & "<br>"
strFileName = getFileNamefromPath(getTextfromBin(sCome,posB,posE-posB))
posB = instrb(posB,binData,getSBfromDb("Content-Type:")) + 14
posE = instrb(posB,binData,bincrlf)
'response.Write "content-type=" & getTextfromBin(sCome,posB,posE-posB) & "<br>"
strContentType = getTextfromBin(sCome,posB,posE-posB)
posB = posE + 4 '這個(gè)地方換了兩行,具體參看輸出的原始二進(jìn)制數(shù)據(jù)
posE = instrb(posB,binData,binSub)
'response.Write "image data="
'saveBin2File sCome,posB,posE-posB-1,server.MapPath("./") & "\test.jpg"
'Response.Write "<img src='test.jpg' border=0><br>"
posFileBegin = posB
posFileLen = posE-posB-1
strValue = strFileName & "," & strContentType & "," & posFileBegin & "," & posFileLen
else
'Response.Write "沒有上傳文件!" & "<br>"
strValue = ""
end if
else
posB = posE + 4 '這個(gè)地方換了兩行,具體參看輸出的原始二進(jìn)制數(shù)據(jù)
posE = instrb(posB,binData,binCrlf)
'Response.Write "value=" & getTextfromBin(sCome,posB,posE-posB) & "<br>" '這個(gè)后面沒有“"”,所以不用-1
strValue = getTextfromBin(sCome,posB,posE-posB)
end if
dicData.Add strName,strValue
posB = posE + 2
posB = instrb(posB,binData,bincrlf) + 2
posB = instrb(posB,binData,getSBfromDB("name=""")) + 6
loop
strTitle = dicData.Item("txtTitle")
aryFileInfo = dicData.Item("filImage")
if aryFileInfo <> "" then '有文件數(shù)據(jù)上傳
aryFileInfo = split(aryFileInfo,",")
strFileName = aryFileInfo(0)
strContentType = aryFileInfo(1)
posFileBegin = aryFileInfo(2)
posFileLen = aryFileInfo(3)
sCome.Position = posFileBegin-1
binData = sCome.Read(posFileLen)
dim cn, rs, sql
set cn = server.CreateObject("adodb.connection")
cn.Open cnstr
set rs = server.CreateObject("adodb.recordset")
sql = "select title,[content-type],image from tblImage2"
rs.Open sql,cn,1,3
rs.AddNew
rs.Fields("title").Value = strTitle
rs.Fields("content-type").Value = strContentType
'數(shù)據(jù)保存到數(shù)據(jù)庫
rs.Fields("image").AppendChunk binData
'如果數(shù)據(jù)以文件形式保存到磁盤上,用下面這句
'saveBin2File sCome,posFileBegin,posFileLen,server.MapPath("./") & strFileName
rs.Update
rs.Close
set rs = nothing
cn.Close
set cn = nothing
Response.Write "<p>文件保存成功!</p>"
else
Response.Write "<p>沒有上傳文件!</p>"
end if
sCome.Close
set sCome = nothing
else
%>
<form id="frmUpload" name="frmUpload" action="<%=Request.ServerVariables("script_name")%>" method="post" target="_self" enctype="multipart/form-data">
<p>標(biāo)題:<input id="txtTitle" type="text" name="txtTitle" size="40"></p>
<p>圖片:<INPUT id="filImage" type="file" name="filImage" size="40"></p>
<INPUT id="btnUpload" type="submit" value="Upload" name="btnUpload">
</form>
<%
end if
%>
</body>
</HTML>