Implementation of ASP uploading image to database function
Generic no-component upload class, the upload process is to save the picture to the specified folder, at the same time save the path to the database field. Show The picture is displayed according to the path field in the Database table. Of course, about the management of the image , such as delete: only delete the path, the actual picture needs to be based on the path through the FSO to delete ... Is there a situation where the picture is saved directly as a field value? The operation of the picture is as familiar as the operation of the data field. The answer is yes, just set the type of the field as an OLE object
Knowledge Points: OLE object fields are used to store data such as Microsoft Word or Microsoft EXCEL documents, pictures , sounds, and other types of binary data that are created in other programs. An OLE object can be linked or embedded in a field in a Microsoft Access table.
First, Design database Testimg.mdb
For ease of debugging, design table Imgurl, where two fields: ID (autonumber, keyword), img (OLE object)
Second, the connection database file conn. ASP
<%
Db_path= "Testimg.mdb"
Set Conn=server. CreateObject ("Adodb.connection")
connstr= "Driver={microsoft Access driver (*.mdb)};d bq=" &server. MapPath (Db_path)
Conn.Open ConnStr
%>
Third, provide a form page to upload pictures upload.html
<form action= "Upload. asp"method=" post "enctype=" Multipart/form-data ">
<input type= "File" name= "Imgurl" >
<input type= "Submit" Name=ok value= "OK" >
</form>
Iv. accept the data and add the record page upload. ASP
<!--#include file= "Conn. ASP--
<%
Formsize=request.totalbytes
Formdata=request.binaryread (Formsize)
BNCRLF=CHRB (&CHRB) (10)
Divider=leftb (FORMDATA,CLNG (INSTRB (FORMDATA,BNCRLF))-1)
DATASTART=INSTRB (FORMDATA,BNCRLF&BNCRLF) +4
DATAEND=INSTRB (Datastart+1,formdata,divider)-datastart
MYDATA=MIDB (Formdata,datastart,dataend)
Set Rs=server.createobject ("Adodb.recordset")
Rs.Open "SELECT * from Imgurl", conn,3,3
Rs.addnew
RS ("img"). AppendChunk MyData
Rs.update
Rs.close
Set rs=nothing
Set conn=nothing
Response.Redirect "index. ASP"
%>
Extract the contents of the Picture field in the Database table to display the Picture page showimg. ASP
<!--#include file= "Conn. ASP--
<%
Set Rs=server.createobject ("Adodb.recordset")
Sql= "SELECT * from Imgurl where id=" &trim (Request ("id"))
Rs.Open sql,conn,1,1
Response.contenttype= "image/*"
Response.BinaryWrite RS ("img"). GetChunk (8000000)
Rs.close
Set rs=nothing
Set conn=nothing
%>
Vi. Display The index of the picture . ASP
<!--#include file= "Conn. ASP--
<%
Strsql= "SELECT * from Imgurl"
Set Rs=server.createobject ("Adodb.recordset")
Rs.Open strsql,conn,1,1
Do Until rs.eof
WHATID=RS ("id")
%>
asp?id=<%=whatid%> ">
<%
Rs.movenext
Loop
%>