Reprint: Using pure ASP code to upload and store the image in the database

Source: Internet
Author: User
Tags contains count file upload integer
Upload | data | Database with pure ASP code to upload and store the image in the database



Using ASP to write Web applications for a long time, will inevitably encounter a variety of problems, including

about how to upload files to the server I am afraid to meet the most problems, especially upload pictures, such as

You want to achieve a "Daily Star" function in your community similar to that provided by the NetEase virtual community,

It is necessary to provide users with the ability to upload photos. Upload picture files to the server can use a variety of free

File Upload component, although the function is very powerful, but because of many cases, we can only make

With free ASP space or renting other people's virtual space, for the first case, we

There is no possibility of using the file upload component at all; As for the second case, we have to pay a lot of

"Silver" is only possible. Unless you have your own virtual host, you can casually on the server

Installing the components that you need is a situation that is elusive to most people. Then I

Are there no way? Oh, the answer is affirmative (certainly, or I can not

Write this article AH). Let's come up with a pure ASP code to achieve the upload of pictures

and the ability to save to the database (and also to display the images from the database to the work on the Web page)

Can).

First, let's familiarize ourselves with the object method that will be used. We used to get the previous page pass

The data that is handed over is generally used by the request object. Similarly, we can also use the Request object

To obtain the uploaded file data, using the Request.BinaryRead () method. And we're going from

The data that is read from the database to the image displayed on the Web page will be used in the following ways:

Request.binarywrite (). When we get the data from the picture and we want to save it to the database,

You cannot manipulate the database directly using the INSERT statement, but instead use the

AppendChunk method, the same, read the image data in the database, to use the GetChunk side

Method. The specific syntax for each method is as follows:

* Request.BinaryRead Syntax:

Variant = Request.BinaryRead (count)

Parameters

Variant

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

Count

Indicates the amount of data to be read from the client, which is less than or equal to the method used

Request.TotalBytes The amount of data obtained.

* Request.binarywrite Syntax:

Request.binarywrite data

Parameters

Data

The packet to write to the client browser.

* Request.TotalBytes Syntax:

Variant = Request.TotalBytes

Parameters

Variant

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

* AppendChunk Grammar

Appends data to a large text, binary data Field, or Parameter object.

Object. AppendChunk Data

Parameters

Object Field or Parameter objects

The data variant, which contains the information appended to the object.

Description

Use the AppendChunk method of a Field or Parameter object to add a long binary or number of characters

It is filled in to the object. In cases where system memory is limited, you can use the AppendChunk method to pair long

Integer values are partial rather than all operations.

* GetChunk Grammar

Returns all or some of the contents of a large text or binary data Field object.

variable = field. GetChunk (Size)

return value

Returns the Variant type.

Parameters

The Size long integer expression equal to the number of bytes or characters to retrieve.

Description

Uses the GetChunk method of a Field object to retrieve some or all of its long binary or character data.

In cases where system memory is limited, you can use the GetChunk method to handle parts rather than all long integers

Value.

The data returned by the GetChunk call is assigned to the variable. If the Size is greater than the remaining data, the

GetChunk only returns the remaining data without padding the "variable" with white space. If the field is blank, the

The GetChunk method returns Null.

Each subsequent GetChunk call retrieves the number that started at the previous GetChunk call stop

According However, if you retrieve data from one field and then set or read another field in the current record

Value, ADO will think that the data has been retrieved from the first field. If you tune again on the first field

With the GetChunk method, ADO interprets the call as a new GetChunk operation and from the beginning of the record

To start reading at. If the other Recordset object is not a copy of the first Recordset object, the

Accessing the fields in it does not break the GetChunk operation.

If the adFldLong bit in the Attributes property of the Field object is set to True, you can

To use the GetChunk method on the field.

If there is no current record when using the Getchunk method on a Field object, an error 3021 is generated

(no current record).

Next, we're going to design our database as a test of our database structure like

Under (ACCESS97):

Field Name Type description

ID Auto Number PRIMARY key value

IMG OLE object to save picture 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 picture data



Now began to formally write our Pure ASP code upload section, first of all, we have a mention

To provide users with the upload interface, you can allow users to select the image to upload. The code is as follows

(upload.htm):


<body>

<center>

<form name= "mainform" enctype= "Multipart/form-data"

action= "Process.asp" method=post>

<input Type=file name=mefile><br>

<input type=submit name=ok value= "OK" >

</form>

</center>

</body>


Note that the black italic part of the code must have this attribute in the form, otherwise there will be no

method to get the data uploaded.

Next, we're going to do the necessary in the process.asp to get the data from the browser

Because the data we get in the process.asp is not just about what we want to upload.

The image of the data, also contains other useless information, we need to eliminate redundant data, and will place

The ACCESS97 picture data is saved to the database, here we take the example of the case. Specific generation

Code as follows (process.asp):

<%

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=s



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.