怎样将图片上传到数据库进行保存
这个范例共包括三个ASP文件和一个数据库(一个表),全部在同一目录下。
1、tblImage 表结构(ACCESS 2000)
sn 自动编号序列号
content-type 文本图片类型
image OLE 对象图片数据
2、SimpleImageToData.asp:上传表单及保存图片到数据库的代码部分,主要文件。
<%@ Language=VBScript %>
<% option explicit %>
<%
'从一个完整路径中析出文件名称
function getFileNamefromPath(strPath)
getFileNamefromPath = mid(strPath,instrrev(strPath,"\")+1)
end function
'定义数据库连接字符串
dim cnstr
cnstr = "driver={Microsoft Access Driver (*.mdb)};dbq=" &
server.MapPath("./upload.mdb")
%>
<%
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) '定义一个单字节的回车换行符
set sCome = server.CreateObject("adodb.stream")
sCome.Type = 1 '指定返回数据类型 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 '调试用:显示提交的所有数据
'response.Write "
" '调试用
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 & "
"
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 & "
"
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 & "
"
'response.Write "文件名:" & getFileNamefromPath(strPath) & "
"
posB = posE
posE = instrb(posB+1,binData,binCrlf)
'response.Write posB & " | " & posE & "
"
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) '此处因为固定的,所以省略查找 :-) 'response.Write "图片类型:" & strContentType & "
"
posB = posE+2
posE = instrb(posB+1,binData,binCrlf)
'response.Write posB & " | " & posE & "
"
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) & "
"
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 "图片保存成功!" & "
"
else
response.Write "没有上传图片!" & "
"
end if
set sGo = nothing
sCome.Close
set sCome = nothing
else
%>
<%
end if
%>
3、ShowImageListFromData.asp
<%@ Language=VBScript %>
<% option explicit %>
<% 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 "
cn.Close set cn = nothing %> | <% dim sn sn = request.QueryString("sn") if sn = "" then response.Write "没有指定图片!" else response.Write "" end if %> |
4、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
%>