帝国软件
  设为首页 加入收藏 关于我们
 
解密帝国网站管理系统
栏 目:
 
您的位置:首页 > 技术文档 > ASP编程
纯ASP上传图像文件到数据库的最佳例子
作者: 发布时间:2005-03-12 来源:
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)
-----------------------------------------------------------------
  
评论】【加入收藏夹】【 】【打印】【关闭
※ 相关链接
 ·使用VB编写纯ASP程序  (2005-03-12)
 ·基于ACCESS数据库的纯asp论坛制作  (2005-03-12)

   栏目导行
  PHP编程
  ASP编程
  ASP.NET编程
  JAVA编程
   站点最新
·致合作伙伴的欢迎信
·媒体报道
·帝国软件合作伙伴计划协议
·DiscuzX2.5会员整合通行证发布
·帝国CMS 7.0版本功能建议收集
·帝国网站管理系统2012年授权购买说
·PHPWind8.7会员整合通行证发布
·[官方插件]帝国CMS-访问统计插件
·[官方插件]帝国CMS-sitemap插件
·[官方插件]帝国CMS内容页评论AJAX分
   类别最新
·在ASP中使用数据库
·使用ASP脚本技术
·通过启动脚本来感受ASP的力量
·学习使用ASP对象和组件
·解析asp的脚本语言
·初看ASP-针对初学者
·ASP开发10条经验总结
·ASP之对象总结
·ASP与数据库应用(给初学者)
·关于学习ASP和编程的28个观点
 
关于帝国 | 广告服务 | 联系我们 | 程序开发 | 网站地图 | 留言板 帝国网站管理系统