Using ASP to write Web applications for a long time, it is inevitable to encounter a variety of problems, including how to upload files to the service
I'm afraid it's the most common problem, especially uploading pictures, like you want to achieve netease in your own community.
Virtual community to provide a "daily one star" function, it is necessary to provide users with the ability to upload photos. Uploading picture files to services
The device can use a variety of free file upload components, although the function is very powerful, but because of many cases, we
You can only use the free space to support ASP or rent other people's virtual space, for the first case, we have not
It is possible to use the file upload component; As for the second case, we have to pay a lot of "silver" to do so. Unless you
Have your own virtual host, you can casually on the server to install their own required components, this situation for the big
Most people are expected to be unreachable. So there's no way we can do that? Oh, the answer is yes (sure of course)
, or I can not write this article AH). Let's take a look at the following to use the pure ASP code to implement the picture
The ability to upload and save to the database (and also to display the images from the database to the Web page).
First, let's familiarize ourselves with the object method that will be used. We used to get the data from the previous page.
Like using the Request object. Similarly, we can use the request object to get the uploaded file data so that
The method used is Request.BinaryRead (). And we're going to read the data from the database and show it to the page.
The methods used are:
Request.binarywrite (). When we get a picture of the data, to save it to the database, you can not directly
Use the INSERT statement to manipulate the database, but to use the AppendChunk method of ADO and, similarly, read out the database
To use the GetChunk method in the picture data. 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 value of the method request.totalbytes
The amount of data.
*request.binarywrite Syntax:
Request.binarywritedata
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 syntax
Appends data to a large text, binary data field, or Parameter object.
Object. Appendchunkdata
Parameters
Objectfield 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 the case of limited system memory, you can use the AppendChunk method for the long integer value
Partially rather than all of the operations.
*getchunk syntax
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 system memory is limited
, you can use the GetChunk method to handle a long integer value that is 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 are from
One field retrieves the data and then sets or reads the value of another field in the current record, and ADO considers that the first field
Retrieves the data from the. If you call the GetChunk method again on the first field, ADO interprets the call as a new Getchu
The NK operation and reads at the beginning of the record. If the other Recordset object is not a copy of the first Recordset object,
Then 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 field can be used Getchun
K method.
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're going to design our database as a test of our database structure as follows (access200
0):
Field Name Type description
ID Auto Number PRIMARY key value
IMG OLE object to save picture data
For the MSSQLServer7, 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 to provide the upload interface
, you can let users select the pictures to upload. The code is as follows
(upload.htm):
<body>
<center>
<form name= "mainform" enctype= "Multipart/form-data" action= "process.asp" method=p
Ost>
<inputtype=filename=mefile><br>
<inputtype=submitname=okvalue= "OK" >
</form>
</center>
</body>
Note that enctype= "Multipart/form-data" must have this attribute in the form, otherwise it will not be uploaded
Come up with the data. Next, we need to do the necessary processing of the data obtained from the browser in the process.asp, because
The data we get in the process.asp not only contains the data that we want to upload the images, but also the packages
With other useless information, we need to remove the redundant data and save the processed image data to the database, which
We take access2000 as an example. The specific code is 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)
Setconngraph=server. CreateObject ("Adodb.connection")
Conngraph.connectionstring= "Driver={microsoftaccessdriver (*.mdb)};D bq=" &server. Ma
Ppath ("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
%>
Okay, so we're going to save the pictures from the last one in the database called Images.mdb, and the rest of the work is to
Displays the picture data from the database to the top of the page. Generally in HTML, display pictures are used tags
, which is It? Oh, in fact, this SRC attribute in addition to the specified path, you can use OH:
So all we have to do is read from the database in the showimg.asp to meet the criteria
Data and return it to the SRC attribute, as shown in the code below (showimg.asp):
<%
Setconngraph=server. CreateObject ("Adodb.connection")
Conngraph.connectionstring= "Driver={microsoftaccessdriver (*.mdb)};D bq=" &
Server. MapPath ("Images.mdb") &