The ASP code realizes the picture uploads and stores in the database

Source: Internet
Author: User
Tags file upload

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 in their own community like NetEase virtual community to provide a "Daily Star" function, it is necessary to provide users with the ability to upload photos. Upload image files to the server can use a variety of free file upload components, although the function is very powerful, but because in many cases, we can only use the free space to support the ASP or rent other people's virtual space, for the first case, we simply do not have the possibility to use file upload components; As for the second case, we have to pay a lot of "silver" to do so. Unless you have a virtual host of your own, you can easily install the components you need on the server, which is impossible for most people. Then we have no way? Oh, the answer is yes (certainly, otherwise I can not write this article AH). Let's take a look at it. Use pure ASP code to achieve the upload of pictures and save to the database function (incidentally also realize the display database of pictures to the Web page function).

First, let's familiarize ourselves with the object method that will be used. The data we use to get the last page passed over is typically used by the request object. In the same way, we can use the request object to get the uploaded file data, using the Request.BinaryRead (). And we want to read out of the database from the image of the data displayed on the page to use the method is:

Request.binarywrite (). When we get the picture data, to save to the database, we can not directly use the INSERT statement to operate the database, but to use the AppendChunk method of ADO, the same, read the image data in the database, to use the GetChunk 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

Append data to large text, binary data Field, or Parameter pair?

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 fill in an object with long binary or character data. In the case of limited system memory, you can use the AppendChunk method to perform partial rather than complete operations on long values.

* 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 the case of limited system memory, you can use the GetChunk method to work with long integer values that are partial rather than full.

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 data that started at the previous GetChunk call stop. However, if you retrieve data from one field and then set or read the value of another field in the current record, ADO thinks that the data has been retrieved from the first field. If you call the GetChunk method again on the first field, ADO interprets the call as a new GetChunk operation and starts at the beginning of the record. If the other Recordset object is not a copy of the first Recordset object, accessing the fields in it does not break the GetChunk operation. If the adFldLong bit in the Attributes property of a Field object is set to True, the GetChunk method can be used for that field. If there is no current record when using the Getchunk method on a Field object, an error 3021 (no current record) is generated. Next, we are going to design our database as a test of our database structure as follows (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 user-provided upload interface, you can let users choose to upload pictures. The code is as follows

(upload.htm):

$#@60;html$#@62;

$#@60;body$#@62;

$#@60;center$#@62;

$#@60;form name= "mainform" enctype= "Multipart/form-data"

Action= "Pro

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.