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

xiaoxiao2021-03-06  20

Implementing images with pure ASP code Uploading into the database We have used data to get the previous page to pass through the data is usually used. 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 Return Value saves to read the data from the client. Count indicates the amount of data to be read from the client, this value is less than or equal to usage

The amount of data obtained by 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 use long binary or character

The object is filled in the object. In the case of limited system memory, you can use the Appendchunk method to long

The whole value is partially instead of 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 part rather than all long integer.

value.

The data returned by the getChunk call will assign it to the "variable". If size is greater than the remaining data, then

GetChunk only returns the remaining data without populating the "variable" with a blank. If the field is empty, then

The getChunk method returns NULL.

Each subsequent GetChunk call will retrieve the number starting from the previous GetChunk call stop.

according to. However, if you retrieve data from a field and then set or read another field in the current record

The value, ADO will consider that data has been retrieved from the first field. If it is adjusted again on the first field

With the getChunk method, ADO will explain the call as a new GetChunk operation and start from the record.

Start reading. If the other Recordset object is not a copy of the first Recordset object,

Visiting the fields do not destroy the getChunk operation.

If the Adfldlong bit in the Attributes property of the Field object is set to True,

Use the getChunk method to this field.

If there is no current record when using the getchunk method on the field object, an error 3021 will be generated

(No current record). Next, we have to design our database, as testing our database structure

Next (Access97):

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 officially prepared our pure ASP code uploaded part, first of all, we have a mention

The user's upload interface is supplied to allow the user to select the picture to upload. code show as below

(UPLOAD.HTM):

Action = "process.asp" method = post>


Attention to the part of the black hemorrhate in the code, be sure to have this attribute in the Form, otherwise,

Method to get data from uploaded.

Next, we must perform the necessary things to get the data obtained from the browser in Process.asp.

Because the data we have obtained in Process.asp not only contains the upload we want.

The data of the picture also contains other useless information, we need to remove redundant data and will

The picture data has been saved in the database, here we take Access97 as an example. Specific generation

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 (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 pictures from the uploaded the image called Images.mdb.

The rest of the job is to display the image data in the database to the web page. General in HT

In ml, the display picture is used to use the tab, that is, , but

Is our picture to be saved to the database, what is the "picture path"? Oh, in fact, this

In addition to the specified path, the SRC property can also be used:

So, what we have to do is to read out in the database from the database in the database in ShowImg.asp.

Data, 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.

To display the picture normally.

The last thing you want to pay attention is that the processing in my process.asp did not take into account the first page.

(UPLOAD.HTM) There are other data, such as , etc. if

With 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 for yourself

The space cannot be worried using all kinds of upload components. What are you waiting for? Try a try. (All the above programs are in WinNT4.0 English, IIS4, Access 97 / MS SQL Server 7.0

Pass through)

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

New Post(0)