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

getfile.htm
-------------------------

 1<html>
 2<head>
 3<title>保存图片到数据库</title>
 4</head>
 5<body>
 6<b>
 7<p></p></b>你可以找个图片试试,保存完毕后会有提示
 8<form action="savetodb.asp" enctype="multipart/form-data" method="POST">
 9<p>Email : <input name="email" size="20" value="[email protected]"/><br/>   
10Picture : <input name="blob" type="file"/><br/>
11<input name="Enter" type="submit"/> </p>
12</form>
13</body>
14</html>

savetodb.asp
----------------------------------

 1   
 2  
 3Response.Buffer = TRUE   
 4Response.Clear   
 5byteCount = Request.TotalBytes   
 6  
 7RequestBin = Request.BinaryRead(byteCount)   
 8Dim UploadRequest   
 9Set UploadRequest = CreateObject("Scripting.Dictionary")   
10  
11BuildUploadRequest RequestBin   
12  
13email = UploadRequest.Item("email").Item("Value")   
14  
15contentType = UploadRequest.Item("blob").Item("ContentType")   
16filepathname = UploadRequest.Item("blob").Item("FileName")   
17filename = Right(filepathname,Len(filepathname)-InstrRev(filepathname,"\"))   
18picture = UploadRequest.Item("blob").Item("Value")   
19  
20'Response.ContentType = contentType   
21'Response.binaryWrite picture   
22  
23set objCn = server.createobject("adodb.connection")   
24set objRst = server.createobject("adodb.recordset")   
25objCn.Open "upload"   
26objrst.Open "pic", objcn, 1,3,2   
27objrst.addnew   
28objrst.fields("filename")=filename   
29objrst.fields("type")="gif"   
30  
31objrst.fields("what").appendchunk picture   
32objrst.update   
33response.write "

<a "="" &="" href='showpic.asp?id="' objrst("id")="">第" &amp; objrst("id") &amp; "个图片。</a>

1"   
2objrst.close   
3  
4objCn.close   
5set objrst=nothing   
6set objcn = nothing   

showpic.asp
----------------------------------------

 1   
 2set objCn = server.createobject("adodb.connection")   
 3set objRst = server.createobject("adodb.recordset")   
 4objCn.Open "upload"   
 5objrst.Open "select what from pic where id=" & request("id"), objcn   
 6  
 7if not objrst.eof then   
 8response.binarywrite objrst("what")   
 9end if   
10  
11objrst.close   
12objCn.close   
13set objrst=nothing   
14set objcn = nothing   

upload.asp
-------------------------------------------

 1   
 2Sub BuildUploadRequest(RequestBin)   
 3'Get the boundary   
 4PosBeg = 1   
 5PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))   
 6boundary = MidB(RequestBin,PosBeg,PosEnd-PosBeg)   
 7boundaryPos = InstrB(1,RequestBin,boundary)   
 8'Get all data inside the boundaries   
 9Do until (boundaryPos=InstrB(RequestBin,boundary & getByteString("--")))   
10'Members variable of objects are put in a dictionary object   
11Dim UploadControl   
12Set UploadControl = CreateObject("Scripting.Dictionary")   
13'Get an object name   
14Pos = InstrB(BoundaryPos,RequestBin,getByteString("Content-Disposition"))   
15Pos = InstrB(Pos,RequestBin,getByteString("name="))   
16PosBeg = Pos+6   
17PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))   
18Name = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))   
19PosFile = InstrB(BoundaryPos,RequestBin,getByteString("filename="))   
20PosBound = InstrB(PosEnd,RequestBin,boundary)   
21'Test if object is of file type   
22If PosFile<>0 AND (PosFile<PosBound) Then   
23'Get Filename, content-type and content of file   
24PosBeg = PosFile + 10   
25PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(34)))   
26FileName = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))   
27'Add filename to dictionary object   
28UploadControl.Add "FileName", FileName   
29Pos = InstrB(PosEnd,RequestBin,getByteString("Content-Type:"))   
30PosBeg = Pos+14   
31PosEnd = InstrB(PosBeg,RequestBin,getByteString(chr(13)))   
32'Add content-type to dictionary object   
33ContentType = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))   
34UploadControl.Add "ContentType",ContentType   
35'Get content of object   
36PosBeg = PosEnd+4   
37PosEnd = InstrB(PosBeg,RequestBin,boundary)-2   
38Value = MidB(RequestBin,PosBeg,PosEnd-PosBeg)   
39Else   
40'Get content of object   
41Pos = InstrB(Pos,RequestBin,getByteString(chr(13)))   
42PosBeg = Pos+4   
43PosEnd = InstrB(PosBeg,RequestBin,boundary)-2   
44Value = getString(MidB(RequestBin,PosBeg,PosEnd-PosBeg))   
45End If   
46'Add content to dictionary object   
47UploadControl.Add "Value" , Value   
48'Add dictionary object to main dictionary   
49UploadRequest.Add name, UploadControl   
50'Loop to next object   
51BoundaryPos=InstrB(BoundaryPos+LenB(boundary),RequestBin,boundary)   
52Loop   
53  
54End Sub   
55  
56'String to byte string conversion   
57Function getByteString(StringStr)   
58For i = 1 to Len(StringStr)   
59char = Mid(StringStr,i,1)   
60getByteString = getByteString & chrB(AscB(char))   
61Next   
62End Function   
63  
64'Byte string to string conversion   
65Function getString(StringBin)   
66getString =""   
67For intCount = 1 to LenB(StringBin)   
68getString = getString & chr(AscB(MidB(StringBin,intCount,1)))   
69Next   
70End Function   

test.mdb(dsn 名称:upload)
----------------------------------------
表pic:
id:自动加
filename:文本
type:文本
what:ole

-----------------------------------------
存成单个文件,放在一个目录下,打开(必须用http://...)getfile.htm
上传一个.gif或.jpg就可以显示了。
对于大文件在显示程序(showpic.asp)中可能会用到循环和getchunk方法。自己去做。记住,由于ASP目前暂时不支持二进行制读写,只能存二进制到数据库中。

Published At
Categories with Web编程
Tagged with
comments powered by Disqus