[[Asp] how to upload images to a database (OLE object)

Source: Internet
Author: User
Generally, no-component upload class saves the image to the specified folder and saves the path to the database fields. The displayed image is displayed based on the path field in the database table. Of course, for image management, such as deleting: only the path is deleted, and the actual image needs to be deleted through FSO according to the path ......

Is there a situation where the image is saved directly as a field value. Operations on images are like operations on data fields. The answer is yes. It only sets the field type as an OLE object.

Knowledge Point: OLE object fields are used to store data such as Microsoft Word or Microsoft Excel documents, images, and sounds, andProgramOther types of binary data created in. OLE objects can be linked or embedded into fields in the Microsoft Access Table.

I. design the database testimg. MDB

To facilitate debugging, design the imgurl table, including two fields: ID (automatic number, keyword), IMG (OLE object)

2. Connect to the database file conn. asp

< %
Db_path = " Testimg. mdb"
Set Conn = Server. Createobject ( " ADODB. Connection " )
Connstr = " Driver = {Microsoft Access Driver (*. mdb)}; DBQ = " & Server. mappath (db_path)
Conn. Open connstr
% >

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 >

4. Accept data and add the record page upload. asp

< ! -- # Include file = " Conn. asp " -->
< %
Formsize = Request. totalbytes
Formdata = Request. binaryread (formsize)
Bncrlf = Chrb ( 13 ) & Chrb ( 10 )
Divider = Leftb (formdata, Clng (Using B (formdata, bncrlf )) - 1 )
Datastart = Using B (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"
% >

5. Extract the image field from the database table to display the showimg. asp image page.

< ! -- # 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
% >

6. display the image index. 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 " )
% >
< IMG SRC = " Showimg. asp? Id = <% = Whatid %> " >

< %
Rs. movenext
Loop % >

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.