Implement images with pure ASP code and store them in the database

xiaoxiao2021-03-06  41

Write a website application with ASP, it is difficult to encounter a wide range of questions, where to upload files to the server, I am probabulous to meet the most problems, especially upload pictures, such as what you want in your own community. Realize the "Daily One Star" functionality provided by Netease Virtual Community, it is necessary to provide users' ability to upload photos. Upload image files to the server can use a variety of free file upload components, which is very powerful, but because in many cases, we can only use the free support ASP space or rent different virtual space for the first situation. We have no possibility to use file upload components; as for the second case, we have to pay a lot of "silver". Unless you have your own virtual host, you can just install your own components on the server, this situation is desirable for most people. Then there is no way? Oh, the answer is sure (of course, it is certain, or I can't write this article). Let's use the pure ASP code to implement the upload of the picture and save to the database.

First let's first familiarize yourself with the object method to use. The data we use to get the previous page is generally used to use the Request object. Similarly, we can also use the REQUEST object to get uploaded file data, and the method used is request.binaryRead (). The data we have to read from the database is displayed to the method you want to use on the web page is: request.binaryWrite (). When we get the picture of the picture, when you want to save to the database, you cannot use the INSERT statement to operate directly using the ADO's Appendchunk method, the same, read the image data in the database, to use Getchunk method. The specific syntax of each method is as follows:

* Request.binaryRead syntax:

Variant = Request.binaryRead (count)

parameter

Variant

The return value is saved to read the data from the client.

count

Indicates the amount of data to be read from the client, which is less than or equal to the amount of data obtained by using the method Request.Totalbytes.

* Request.binaryWrite Syntax:

Request.BinaryWrite Data

parameter

Data

To write the packets in the client browser.

* Request.TotalBytes syntax:

Variant = Request.totalBytes

parameter

Variant

Returns the number of bytes from the client reading to the amount of data.

* Appendchunk syntax

Add data to large text, binary data field or parameter object.

Object.Appendchunk Data

parameter

Object Field or Parameter object

Data variants, including data added to the object.

Description

The Appendchunk method using the field or parameter object can be filled in the object in the object. In the case where the system is limited, the Appendchunk method can be used to partially perform a portion rather than all operations.

* GetChunk syntax

Returns all or part of the large text or binary data Field object.

Variable = Field.getChunk (size)

return value

Return the variable body.

parameter

Size long integer expressions, equal to the number of bytes or characters to be retrieved.

Description

Some or all long binary or character data are retrieved using the getchunk method of the field object. In the case where the system is limited, the GetChunk method can be used to handle the portion instead of all long integer values. The data returned by the getChunk call will assign it to the "variable". If size is greater than the remaining data, getChunk only returns the remaining data without populating the "variable" with a blank. If the field is empty, the getChunk method returns NULL. Each subsequent GetChunk call will retrieve data starting from the previous GetChunk call stop. However, if the data is retrieved from a field and then set or read in the current record, the ADO will consider the data from the first field. If the getchunk method is called again on the first field, the ADO will interpret the call as a new GetChunk operation and start reading from the beginning of the record. If other Recordset objects are not a copy of the first RECORDSET object, the fields that are accessed do not destroy the getChunk operation. If the Adfldlong bit in the Attributes property of the Field object is set to True, you can use the getChunk method for this field. If there is no current record when using a getchunk method on the field object, an error 3021 (no current record) is generated.

Next, we have to design our database, as a test of our database structure (Access 97):

Field Name Type Description

ID Auto Number Primary key value

IMG OLE object is used to save image data

For the MS SQL Server7, the corresponding structure is as follows:

Field Name Type Description

ID INT (Identity) primary key value

IMG image is used to save image data

Now start formal writing our pure ASP code upload section, first, we have an upload interface that provides users to allow users to select the image to upload. code show as below

(UPLOAD.HTM):

Action = "process.asp" method = post>


Note that the part of the black slope in the code must have this attribute in the Form, otherwise, the uploaded data will not be obtained. Next, we have to perform the necessary processing from the data obtained from the browser in process.asp, because the data we have obtained in Process.asp contains not only the data of the uploaded picture we want, Contains other useless information, we need to remove redundant data and save the process data into the database, here we take Access 97 as an example. The specific 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 (INSTRB (FormData, Bncrlf) - 1)

DataStart = INSTRB (Formdata, Bncrlf & Bncrlf) 4

DataEnd = INSTRB (Datastart 1, Formdata, Divider) -DataStart

MyData = MIDB (FormData, Datast, DataEnd)

SET CONNGRAPH = Server.createObject ("AdoDb.Connection")

Conngraph.connectionstring = "driver = {Microsoft Access Driver (* .mdb)}; DBQ =" &

Server.mappath ("images.mdb") & "; uid =; pwd =;"

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

%>

Ok, let's save the uploaded pictures to the database named Images.mdb, the rest of the job is to display the image data in the database to the web page. Generally in HTML, the display picture is used to use the tag, that is, , but our picture is saved in the database, "Image Path"? Oh, in fact, this SRC property can be used in addition to the specified path:

So, what we have to do is read out in the database in ShowImg.asp, and return to the src property, the specific code is as follows: SHOWIMG.ASP:

<%

SET conngraph = server.createObject ("adodb.connection" conngraph.connectionstring = "driver = {Microsoft Access Driver (* .mdb)}; dbq =" &

Server.mappath ("images.mdb") & "; uid =; pwd =;"

Conngraph.Open

SET REC = Server.createObject ("AdoDb.Recordset")

strsql = "SELECT IMG FROMAGES 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 Be sure to specify response.contenttype = "image / *" before outputting to the browser, so that the image is displayed normally. The last thing to pay attention is that the processing in my process.asp does not take into account other data in the UPLoad.htm, such as , etc., if these items, Your process.asp should pay attention to handling unnecessary data.

How, actually upload pictures and saved to the database very simple, so no need to use all kinds of upload components in your own space. What are you waiting for? Try a try. (All of the above programs are running in WINNT4.0 English, IIS4, Access97 / MS SQL Server7.0)

转载请注明原文地址:https://www.9cbs.com/read-64802.html

New Post(0)