The best example of a pure ASP uploading image files to a database

Source: Internet
Author: User
Tags chr
Getfile.htm
-------------------------

<title> save pictures to database </title>

<body>
<b>

<p></b> you can find a picture to try, after saving will be prompted </p>

<form method= "POST" enctype= "Multipart/form-data" action= "savetodb.asp" >
<p>email: <input name= "Email" value= "wangcq@sina.com" size= "><br>"
Picture: <input type= "file" Name= "blob" ><br>
<input type= "Submit" Name= "Enter" > </p>
</form>
</body>

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") & "> First" & Objrst ("id") & "picture. </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 the objects are put in a Dictionary object
Dim Uploadcontrol
Set Uploadcontrol = CreateObject ("Scripting.Dictionary")
' Get ' 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 name: Upload)
----------------------------------------
Table Pic:
ID: Auto Add
FileName: Text
Type: Text
What:ole

-----------------------------------------
Save as a single file, placed in a directory, open (must use http://...) ) getfile.htm
Uploading a. gif or. jpg can be displayed.
The loops and GetChunk methods may be used in the display program (showpic.asp) for large files. Do it yourself. Remember, because the ASP currently does not support the second to read and write, can only save binary to the database.


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.