Use ASP code to upload images

Source: Internet
Author: User

It takes a long time to write website applications using ASP, And you will inevitably encounter various problems. I am afraid that the most common problems are problems concerning how to upload files to the server, especially uploading images, for example, if you want to implement the "one star per day" function similar to that provided by Netease virtual community in your own community, you need to provide users with the photo upload function. You can use a variety of free File Upload components to upload image files to the server. The functions are powerful, but in many cases, we can only use free space that supports ASP or rent others' virtual space. In the first case, we cannot use the File Upload Component. In the second case, we also need to pay a lot of money. Unless you have your own virtual host, you can install the components you need on the server. This situation is beyond reach for most people. So we have no choice? Haha, the answer is yes (Of course yes, otherwise I cannot write this article ). Next let's use the pure ASP code to upload images and save them to the database (by the way, we can also display images in the database to the web page ).

 

First, familiarize yourself with the object methods to be used. We use the request object to obtain the data transmitted from the previous page. Similarly, we can use the request object to obtain the uploaded file data, using request. binaryread (). However, to read the image data from the database and display it on the webpage, we need to use the following methods:
Request. binarywrite (). When we get the image data and want to save it to the database, we cannot directly use the insert statement to operate the database, but use the AppendChunk method of ADO. Similarly, to read the image data in the database, use the getchunk method. The syntax of each method is as follows:
* Request. binaryread Syntax:
Variant = request. binaryread (count)
Parameters
Variant
The return value stores the data read from the client.
Count
Indicates the size of the data to be read from the client. The value is smaller than or equal to the data size obtained by using request. totalbytes.
* Request. binarywrite Syntax:
Request. binarywritedata
Parameters
Data
Data packets to be written to the client browser.
* Request. totalbytes Syntax:
Variant = request. totalbytes
Parameters
Variant
Returns the number of bytes of data read from the client.
* AppendChunk syntax
Append data to a large text, binary data field, or parameter object.
Object. appendchunkdata
Parameters
Objectfield or parameter object
Data changes, including the data appended to the object.
Description
You can use the AppendChunk method of the field or parameter object to convert the long binary or number of characters
Enter data in the object. If the system memory is limited, you can use the AppendChunk method to partially and not fully operate long integer values.
* Getchunk syntax
Returns all or part of the field object of large text or binary data.
Variable = field. getchunk (size)
Return Value
Returns the variant.
Parameters
Size long integer expression, which is equal to the number of bytes or characters to be retrieved.
Description
Use the getchunk method of the Field object to retrieve partial or full-length binary or character data. If the system memory is limited, you can use the getchunk method to process partial rather than all long integer values.
The data returned by the getchunk call is assigned to the variable ". If the size is greater than the remaining data
Getchunk only returns the remaining data without filling in the "variable" in the blank space ". If the field is empty
The getchunk method returns NULL.
Each subsequent getchunk call will retrieve the data starting from the stop of the previous getchunk call. However, if you retrieve data from a field and then set or read the value of another field in the current record, ADO considers that the data has been retrieved from the first field. If the getchunk method is called again on the first field, ADO interprets the call as a new getchunk operation and reads it from the beginning of the record. If other recordset objects are not copies of the first recordset object, accessing the fields in them will not destroy the getchunk operation.
If the adfldlong bit in the attributes attribute of the Field object is set to true, you can use the getchunk method for this field.
If no current record exists when the getchunk method is used on the Field object, error 3021 will be generated (no current record ).
Next, we will design our database. As a test, our database structure is as follows (Access2000 ):

 

Field name type description
Id auto-numbered primary key value
The img ole object is used to save image data.

 

In mssqlserver7, 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, we have officially compiled the pure ASP code upload part. First, we have an upload interface provided to users, allowing users to select the image to be uploaded. The Code is as follows:
(Upload.htm ):
<HTML>
<Body>
<Center>
<Form name = "mainform" enctype = "multipart/form-Data" Action = "process. asp" method = post>
<Inputtype = filename = mefile> <br>
<Inputtype = submitname = okvalue = "OK">
</Form>
</Center>
</Body>
</Html>
Note that enctype = "multipart/form-Data" must have this attribute in form. Otherwise, the uploaded data cannot be obtained. Next, we will go to process. ASP makes necessary processing for the data obtained from the browser, because we are in process. the data obtained in ASP not only contains the data of the images we want to upload, but also contains other useless information. We need to remove redundant data, and save the processed image data to the database. Here we take Access2000 as an example. 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 (partition B (formdata, bncrlf)-1)
Datastart = instrb (formdata, bncrlf & bncrlf) + 4
Dataend = Consumer B (datastart + 1, formdata, divider)-datastart
Mydata = midb (formdata, datastart, dataend)
Setconngraph = server. Createobject ("ADODB. Connection ")
Conngraph. connectionstring = "driver = {microsoftaccessdriver (*. mdb)}; DBQ =" & server. mappath ("images. mdb") & "; uid =; Pwd = ;"
Conngraph. Open
Setrec = server. Createobject ("ADODB. recordset ")
Rec. Open "select * from [images] whereidisnull", conngraph, 1, 3
Rec. addnew
Rec ("IMG"). appendchunkmydata
Rec. Update
Rec. Close
Setrec = nothing
Setconngraph = nothing
%>
Now we have saved the uploaded image to the database named images. MDB. The rest is to display the image data in the database to the webpage. Generally, labels are used to display images in HTML, that is, , but our images are saved to the database, what is "image path? In fact, in addition to specifying the path, this src attribute can also be used as follows:

Therefore, all we need to do is read the qualified ones from the database in showimg. asp.
The data is returned to the src attribute. The specific code is as follows (showimg. asp ):
<%
Setconngraph = server. Createobject ("ADODB. Connection ")
Conngraph. connectionstring = "driver = {microsoftaccessdriver (*. mdb)}; DBQ = "&
Server. mappath ("images. mdb") & "; uid =; Pwd = ;"
Conngraph. Open
Setrec = server. Createobject ("ADODB. recordset ")
Strsql = "selectimgfromimageswhereid =" & trim (Request ("ID "))
Rec. openstrsql, conngraph, 1, 1
Response. contenttype = "image /*"
Response. binarywriterec ("IMG"). getchunk (7500000)
Rec. Close
Setrec = nothing
Setconngraph = nothing
%>
Note that you must specify response. contenttype = "image/*" before outputting data to the browser /*",
To display images normally.
Most importantly, the processing in process.aspdoes not include other data in the first page (upload.htm), such as <input type = tesxt name = userid>. If you have these projects, process. asp must process unnecessary data.

 

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.