ASP上傳功能的實(shí)例分析
本文分享給大家的是關(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 自動編號 序列號
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>