纯ASP上传图像文件到数据库的最佳例子
敬业的IT人
互联网
佚名
2008-4-14 17:01:35
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=http://www.cncms.com.cn/asp/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.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=http://www.cncms.com.cn/asp/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")
- 最新文章
- 数组数据排序的程序例子[04-14]
- 网络精英计数器源程序[04-14]
- 全文本检索的应用(1)[04-14]
- 全文本检索的应用(2)[04-14]
- 全文本检索的应用(3)[04-14]
- 通过ASP与ACCESS数据库建立连接(附源码)(1)[04-14]
- 相关文章
- 通过ASP与ACCESS数据库建立连接(附源码)(1)[04-14]
- 通过ASP与ACCESS数据库建立连接(附源码)(2)[04-14]
- 通过ASP与ACCESS数据库建立连接(附源码)(3)[04-14]
- 仿照CHINAASP论坛中TOP10写的部分显示代码[04-14]
- 用ASP做一个记事本编缉器(附源码)[04-14]
- 用ASP发 WAP MAIL(-)[04-14]
