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):