iamtrue 发表于2000-11-2 7:56:49 asp地带
getfile.htm
————————-
<html>
<head>
<title>保存图片到数据库</title>
</head>
<body>
<b>
<p></b>你可以找个图片试试,保存完毕后会有提示</p>
<form method="post" enctype="multipart/form-data" action="savetodb.asp">
<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")
get an object name
pos = instrb(boundarypos,requestbin,getbytestring("content-disposition"))
pos = instrb(pos,requestbin,getbytestring("name="))
posbeg = pos+6
posend = instrb(posbeg,requestbin,getbytestring(chr(34)))
name = getstring(midb(requestbin,posbeg,posend-posbeg))
posfile = instrb(boundarypos,requestbin,getbytestring("filename="))
posbound = instrb(posend,requestbin,boundary)
test if object is of file type
if posfile<>0 and (posfile<posbound) then
get filename, content-type and content of file
posbeg = posfile + 10
posend = instrb(posbeg,requestbin,getbytestring(chr(34)))
filename = getstring(midb(requestbin,posbeg,posend-posbeg))
add filename to dictionary object
uploadcontrol.add "filename", filename
pos = instrb(posend,requestbin,getbytestring("content-type:"))
posbeg = pos+14
posend = instrb(posbeg,requestbin,getbytestring(chr(13)))
add content-type to dictionary object
contenttype = getstring(midb(requestbin,posbeg,posend-posbeg))
uploadcontrol.add "contenttype",contenttype
get content of object
posbeg = posend+4
posend = instrb(posbeg,requestbin,boundary)-2
value = midb(requestbin,posbeg,posend-posbeg)
else
get content of object
pos = instrb(pos,requestbin,getbytestring(chr(13)))
posbeg = pos+4
posend = instrb(posbeg,requestbin,boundary)-2
value = getstring(midb(requestbin,posbeg,posend-posbeg))
end if
add content to dictionary object
uploadcontrol.add "value" , value
add dictionary object to main dictionary
uploadrequest.add name, uploadcontrol
loop to next object
boundarypos=instrb(boundarypos+lenb(boundary),requestbin,boundary)
loop
end sub
string to byte string conversion
function getbytestring(stringstr)
for i = 1 to len(stringstr)
char = mid(stringstr,i,1)
getbytestring = getbytestring & chrb(ascb(char))
next
end function
byte string to string conversion
function getstring(stringbin)
getstring =""
for intcount = 1 to lenb(stringbin)
getstring = getstring & chr(ascb(midb(stringbin,intcount,1)))
next
end function
%>
test.mdb(dsn 名称:upload)
—————————————-
表pic:
id:自动加
filename:文本
type:文本
what:ole
—————————————–
存成单个文件,放在一个目录下,打开(必须用http://…)getfile.htm
上传一个.gif或.jpg就可以显示了。
对于大文件在显示程序(showpic.asp)中可能会用到循环和getchunk方法。自己去做。记住,由于asp目前暂时不支持二进行制读写,只能存二进制到数据库中。
有什么问题,请致电veryblue@chinese.com (iamtrue)
—————————————————————–
