您的位置:首页网页设计ASP实例 → 纯ASP上传图像文件到数据库的最佳例子

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

时间:2004/11/7 2:51:00来源:本站整理作者:蓝点我要评论(0)

getfile.htm

-------------------------






保存图片到<a href="http://www.baidu.com/baidu?tn=sayyes&word=数据库" target="_blank" _href="http://www.baidu.com/baidu?tn=sayyes&word=数据库"><span class="unnamed8"><span style="color:#0000FF">数据库</span></span></a>













 

你可以找个图片试试,保存完毕后会有提示




  

Email :


  Picture :


  










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 "第" & objrst("id") & "个图片。"

objrst.close



objCn.close

set objrst=nothing

set objcn = nothing

%>





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目前暂时不支持二进行制读写,只能存二进制到数据库中。

相关阅读 Windows错误代码大全 Windows错误代码查询激活windows有什么用Mac QQ和Windows QQ聊天记录怎么合并 Mac QQ和Windows QQ聊天记录Windows 10自动更新怎么关闭 如何关闭Windows 10自动更新windows 10 rs4快速预览版17017下载错误问题Win10秋季创意者更新16291更新了什么 win10 16291更新内容windows10秋季创意者更新时间 windows10秋季创意者更新内容kb3150513补丁更新了什么 Windows 10补丁kb3150513是什么

文章评论
发表评论

热门文章 没有查询到任何记录。

最新文章 迅雷新手完全入门手册 asp下面javascript上传图片限制格式大小方法告诉大家网页弹出窗口应用总结ASP常见错误类型大全asp常见错误分析和解决办法

人气排行 总是弹出visual studio 实时调试器 三种解决SQLSERVER存储过程及调用详解Asp获取真实IP地址ASP中连接Mssql的几种方法一个简单好用的UBB编辑器(含代码)如何用Split将字符串转换为数组并获取数组下ASP防止表单重复提交的办法告诉你免费的简单聊天室源代码