欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

纯ASP上传图像文件到数据库的最佳例子

程序员文章站 2022-04-15 11:03:52
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")