纯ASP上传图像文件到数据库的最佳例子
程序员文章站
2022-08-09 18:43:20
getfile.htm-------------------------保存图片到 ...
getfile.htm
-------------------------
<html>
<head>
<title>保存图片到</title>
</head>
<body>
<b>
<p></b>你可以找个图片试试,保存完毕后会有提示</p>
<form method="post" enctype="multipart/form-data" action="savetodb.">
<p>email : <input name="email" value="wangcq@sina.com" size="20"><br>
picture : <input type="file" name="blob"><br>
<input type="submit" name="enter"> </p>
</form>
</body>
</html>
savetodb.asp
----------------------------------
<%
response.buffer = true
response.clear
bytecount = request.totalbytes
requestbin = request.binaryread(bytecount)
dim uploadrequest
set uploadrequest = createobject("scripting.dictionary")
builduploadrequest requestbin
email = uploadrequest.item("email").item("value")
contenttype = uploadrequest.item("blob").item("contenttype")
filepathname = uploadrequest.item("blob").item("filename")
filename = right(filepathname,len(filepathname)-instrrev(filepathname,""))
picture = uploadrequest.item("blob").item("value")
response.contenttype = contenttype
response.binarywrite picture
set objcn = server.createobject("adodb.connection")
set objrst = server.createobject("adodb.recordset")
objcn.open "upload"
objrst.open "pic", objcn, 1,3,2
objrst.addnew
objrst.fields("filename")=filename
objrst.fields("type")="gif"
objrst.fields("what").appendchunk picture
objrst.update
response.write "<a href=showpic.asp?id=" & objrst("id") & ">第" & objrst("id") & "个图片。</a>"
objrst.close
objcn.close
set objrst=nothing
set objcn = nothing
%>
<!--#include file="upload.asp"-->
showpic.asp
----------------------------------------
<%
set objcn = server.createobject("adodb.connection")
set objrst = server.createobject("adodb.recordset")
objcn.open "upload"
objrst.open "select what from pic where id=" & request("id"), objcn
if not objrst.eof then
response.binarywrite objrst("what")
end if
objrst.close
objcn.close
set objrst=nothing
set objcn = nothing
%>
upload.asp
-------------------------------------------
<%
sub builduploadrequest(requestbin)
get the boundary
posbeg = 1
posend = instrb(posbeg,requestbin,getbytestring(chr(13)))
boundary = midb(requestbin,posbeg,posend-posbeg)
boundarypos = instrb(1,requestbin,boundary)
get all data inside the boundaries
do until (boundarypos=instrb(requestbin,boundary & getbytestring("--")))
members variable of objects are put in a dictionary object
dim uploadcontrol
set uploadcontrol = createobject("scripting.dictionary")
-------------------------
<html>
<head>
<title>保存图片到</title>
</head>
<body>
<b>
<p></b>你可以找个图片试试,保存完毕后会有提示</p>
<form method="post" enctype="multipart/form-data" action="savetodb.">
<p>email : <input name="email" value="wangcq@sina.com" size="20"><br>
picture : <input type="file" name="blob"><br>
<input type="submit" name="enter"> </p>
</form>
</body>
</html>
savetodb.asp
----------------------------------
<%
response.buffer = true
response.clear
bytecount = request.totalbytes
requestbin = request.binaryread(bytecount)
dim uploadrequest
set uploadrequest = createobject("scripting.dictionary")
builduploadrequest requestbin
email = uploadrequest.item("email").item("value")
contenttype = uploadrequest.item("blob").item("contenttype")
filepathname = uploadrequest.item("blob").item("filename")
filename = right(filepathname,len(filepathname)-instrrev(filepathname,""))
picture = uploadrequest.item("blob").item("value")
response.contenttype = contenttype
response.binarywrite picture
set objcn = server.createobject("adodb.connection")
set objrst = server.createobject("adodb.recordset")
objcn.open "upload"
objrst.open "pic", objcn, 1,3,2
objrst.addnew
objrst.fields("filename")=filename
objrst.fields("type")="gif"
objrst.fields("what").appendchunk picture
objrst.update
response.write "<a href=showpic.asp?id=" & objrst("id") & ">第" & objrst("id") & "个图片。</a>"
objrst.close
objcn.close
set objrst=nothing
set objcn = nothing
%>
<!--#include file="upload.asp"-->
showpic.asp
----------------------------------------
<%
set objcn = server.createobject("adodb.connection")
set objrst = server.createobject("adodb.recordset")
objcn.open "upload"
objrst.open "select what from pic where id=" & request("id"), objcn
if not objrst.eof then
response.binarywrite objrst("what")
end if
objrst.close
objcn.close
set objrst=nothing
set objcn = nothing
%>
upload.asp
-------------------------------------------
<%
sub builduploadrequest(requestbin)
get the boundary
posbeg = 1
posend = instrb(posbeg,requestbin,getbytestring(chr(13)))
boundary = midb(requestbin,posbeg,posend-posbeg)
boundarypos = instrb(1,requestbin,boundary)
get all data inside the boundaries
do until (boundarypos=instrb(requestbin,boundary & getbytestring("--")))
members variable of objects are put in a dictionary object
dim uploadcontrol
set uploadcontrol = createobject("scripting.dictionary")
上一篇: 学习使用ASP对象和组件
下一篇: mybatis学习使用4动态sql