How does csdn store images in a database?

Source: Internet
Author: User
If you do not want others to easily reference your images on other sites, you can store the images in the database. the following describes how to upload images to the database, how to display the database, and how to add verification.
First, familiarize yourself with the object methods to be used. We use the request object to obtain the data transmitted from the previous page. Similarly, we can use the request object to obtain the uploaded file data, using request. binaryread (). However, to read the image data from the database and display it on the webpage, we need to use the following methods:
Request. binarywrite (). When we get the image data and want to save it to the database, we cannot directly use the insert statement to operate the database, but use the AppendChunk method of ADO. Similarly, to read the image data in the database, use the getchunk method. The syntax of each method is as follows:

1) Request. binaryread Syntax:
Variant = request. binaryread (count)
Parameter: The variant return value stores the data read from the client.
Count indicates the amount of data to be read from the client. The value is smaller than or equal to the amount of data obtained using request. totalbytes.

2) request. binarywrite Syntax:
Request. binarywrite data
Parameter: data is the data packet to be written to the client browser.

3) request. totalbytes Syntax:
Variant = request. totalbytes
Parameter: variant returns the number of bytes read from the client to the data size.

4) AppendChunk syntax
Append data to a large text, binary data field, or parameter object.
Object. AppendChunk data
Parameters:
Object field or parameter object
Data changes, including the data appended to the object.
Description
You can use the AppendChunk method of the field or parameter object to enter long binary or character data into the object. If the system memory is limited, you can use the AppendChunk method to partially and not fully operate long integer values.

5) getchunk syntax
Returns all or part of the field object of large text or binary data.
Variable = field. getchunk (size)
Return Value
Returns the variant.
Parameter: size long integer expression, which is equal to the number of bytes or characters to be retrieved.
Note:
Use the getchunk method of the Field object to retrieve partial or full-length binary or character data. If the system memory is limited, you can use the getchunk method to process partial rather than all long integer values.
The data returned by the getchunk call is assigned to the variable ". If the size is greater than the remaining data, getchunk returns only the remaining data without filling in the "variable" in the blank space ". If the field is empty
The getchunk method returns NULL. Each subsequent getchunk call will retrieve the data starting from the stop of the previous getchunk call. However, if you retrieve data from a field and then set or read the value of another field in the current record, ADO considers that the data has been retrieved from the first field. If the getchunk method is called again on the first field, ADO interprets the call as a new getchunk operation and reads it from the beginning of the record. If other recordset objects are not copies of the first recordset object, accessing the fields in them will not destroy the getchunk operation. If the adfldlong bit in the attributes attribute of the Field object is set to true, you can use the getchunk method for this field.
If no current record exists when the getchunk method is used on the Field object, error 3021 will be generated (no current record ).

Next, we will design our database. As a test, our database structure is as follows (ACCESS ):
Field name type description
Id auto-numbered primary key value
The img ole object is used to save image data.
Now we are writing our pure ASPCodeUpload part. First, we have an upload interface for users to select the image to upload. The Code is as follows:
(Upload.htm ):
<HTML>
<Body>
<Center>
<Form name = "mainform" enctype = "multipart/form-Data" Action = "process. asp" method = post>
<Input type = file name = upfile> <br>
<Input type = submit name = OK value = "OK">
</Form>
</Center>
</Body>
</Html>

Note that <enctype = "multipart/form-Data"> in the Code must have this attribute in form. Otherwise, the uploaded data cannot be obtained.
Next, we will go to process. ASP makes necessary processing for the data obtained from the browser, because we are in process. the data obtained in ASP not only contains the data of the images we want to upload, but also contains other useless information. We need to remove redundant data, and save the processed image data to the database. Here we take access as an example. The Code is as follows (process. asp ):
<%
Response. Buffer = true
Formsize = request. totalbytes
Formdata = request. binaryread (formsize)
Bncrlf = chrb (13) & chrb (10)
Divider = leftb (formdata, clng (partition B (formdata, bncrlf)-1)
Datastart = instrb (formdata, bncrlf & bncrlf) + 4
Dataend = Consumer B (datastart + 1, formdata, divider)-datastart
Mydata = midb (formdata, datastart, dataend)
Set conngraph = server. Createobject ("ADODB. Connection ")
Conngraph. connectionstring = "provider = Microsoft. Jet. oledb.4.0; Data Source =" & server. mappath ("images. mdb ")
Conngraph. Open
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. Close
Set rec = nothing
Set conngraph = nothing
%>

Now we have saved the uploaded image to the database named images. MDB. The rest is to display the image data in the database to the webpage. Generally, labels are used to display images in HTML, that is, , but our images are saved to the database, what is "image path? In fact, in addition to specifying the path, this src attribute can also be used as follows:

Therefore, all we have to do is go to showimg. ASP reads qualified data from the database and returns the data to the src attribute. The specific code is as follows (showimg. ASP ):
<%
Fromwhere = lcase (request. servervariables ("SERVER_NAME "))
If instr (fromwhere, "arhwen.com") = 0 then ', determine whether to access the website. If not, stop the following execution. This prevents leeching.
Response. End
End if

Set conngraph = server. Createobject ("ADODB. Connection ")
Conngraph. connectionstring = "provider = Microsoft. Jet. oledb.4.0; Data Source =" & server. mappath ("images. mdb ")
Conngraph. Open
Set rec = server. Createobject ("ADODB. recordset ")
Strsql = "select IMG from images where id =" & trim (Request ("ID "))
Rec. Open strsql, conngraph, 1, 1
Response. contenttype = "image /*"
Response. binarywrite Rec ("IMG"). getchunk (7500000)
Rec. Close
Set rec = nothing
Set conngraph = nothing
%>

Note that you must specify response. contenttype = "image/*" before outputting the image to the browser to display the image normally.
Through such processing, I believe it is not easy to steal your images.

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.