This article explains how to upload and display pictures uploaded on an Access database from a number of perspectives.
In the process of making a dynamic Web site, you need to upload pictures, display pictures, upload pictures to be able to save in the database, the General small Web site only support Access database, how to upload pictures, save it in the database and display it, is one of the database +asp application technology. Many books describe only the methods of the SQL database and are rarely mentioned in access. There are a lot of access to the Internet database upload pictures, display pictures of the article, most of the article introduced not comprehensive, some procedures simply can not open, for beginners is difficult to grasp, I by reference to some articles, to provide a more complete set of procedures for beginners, A more comprehensive description of the use of upload to access database to add pictures, display pictures, I hope that our novice comrades to take less detours.
The first step is to make an Access database, we name it Images.mdb, there are two fields in the database: ID, the type of the Img,id field is set to AutoNumber, and the IMG field type is set to OLE object.
The second step, the design of an uploaded form, according to the requirements can be simplified.
The third step, the background of the design of the image processing program, can be divided into picture upload save program and Picture Reader program.
The fourth step, the picture shows.
The following is the program and introduced separately.
First, the form procedure updata.html
Its function is to provide an interface for uploading, the form's Enctype property is Multipart/form-data, it is to set the MIME encoding of the form, only use it to completely transfer the data of the file.
The following are the referenced contents: action= "process.asp" method= "POST" |
Second, the picture upload and save the program process.asp
The following are the referenced contents: <% Response.buffer=true 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 Conngraph=server. CreateObject ("Adodb.connection") Conngraph.open "Provider=Microsoft.Jet.OLEDB.4.0; Data source= "& Server. MapPath ("Images.mdb") Set Rec=server.createobject ("Adodb.recordset") Rec. Open "SELECT * from images where ID is null", conngraph,1,3 Rec.addnew REC ("img"). AppendChunk MyData Rec.update Rec.closes Set rec=nothing Set Conngraph=nothing%> |
The function of this program is to save the uploaded image data to the database. The following clause explains the function of each statement.
The following are the referenced contents: Response.buffer=true Formsize=request.totalbytes Formdata=request.binaryread (Formsize) |
Turn on the caching function, get the total amount of data sent by the client, get the data uploaded over.
The following are the referenced contents: BNCRLF=CHRB (+) & ChrB (10) Divider=leftb (FORMDATA,CLNG (INSTRB (FORMDATA,BNCRLF))-1) |
The two statements mean to set a binary carriage return and get a divider delimiter to determine the position of the picture.
The following are the referenced contents: DATASTART=INSTRB (Formdata,bncrlf & Bncrlf) +4 DATAEND=INSTRB (Datastart+1,formdata,divider)-datastart MYDATA=MIDB (Formdata,datastart,dataend) |
The above three statements are data that determines the starting position, end position, and actual picture of the picture data.
The following are the referenced contents: Set Conngraph=server. CreateObject ("Adodb.connection") Conngraph.open "Provider=Microsoft.Jet.OLEDB.4.0; Data source= "& Server. MapPath ("Images.mdb") |
Create the Connection object and connect to the Microsoft Access database.
The following are the referenced contents: Set Rec=server.createobject ("Adodb.recordset") Rec. Open "SELECT * from images where ID is null", conngraph,1,3 Rec.addnew REC ("img"). AppendChunk MyData |
Create a Recordset object, open the database and write state, execute rec.addnew Add a new record, call the Field object's AppendChunk method to save the picture data to a field in the database table.
The following sentence closes the database, releasing the settings that define the component.
Third, the picture's reading program showing.asp
The following are the referenced contents: <% Set Conngraph=server. CreateObject ("Adodb.connection") Conngraph.open "Provider=Microsoft.Jet.OLEDB.4.0; Data source= "& Server. MapPath ("Images.mdb") Set Rec=server.createobject ("Adodb.recordset") Id=trim (Request.QueryString ("id")) Strsql= "select img from images where id=" &request.querystring ("id") & "" Rec.open strsql,conngraph,1,1 Response.ContentType = "Image/gif" Response.BinaryWrite Rec ("img"). GetChunk (7500000) Rec.close Set rec=nothing Set conngraph=nothing %> |
The above program is the background program that displays pictures, the main function is to read the data of the picture in the database according to the required ID number.
Response.BinaryWrite Rec ("img"). GetChunk (7500000)
This calls the GetChunk (size) method of the Field object, and the size is the number of bytes.
What you need to be aware of is the MIME type setting when using the ContentType property of the response object, where we set the type of the returned data to a graphical, image/gif way, which can display a GIF or JPG graphic, if set to image/*, The program will not be able to display the picture when it executes.
Four, the picture display program show.html
Picture upload saved to the database can call the program to display the picture, we do a form program, provide the ID number to display the picture.
The following are the referenced contents: action= "showing.asp" method= "get" > |
Please enter the serial number to display the picture:
The following are the referenced contents: |
The above program and database system can be uploaded to the server after the use, can also be used on the local server, but the confidential installation of Ⅱs Plug-ins.