Using ASP to write a website application for a long time, will inevitably encounter a variety of problems, including
about how to upload files to the server is probably the most encountered problems, especially uploading pictures, such as
You want to achieve a "one star" feature in your community similar to that offered by NetEase virtual community,
It is necessary to provide users with the ability to upload photos. Uploading image files to the server can use a variety of free
file Upload component, although the use of the function is very powerful, but because in many cases, we can only make
with free space to support ASP or renting someone else's virtual space, for the first case, we  
There is no possibility to use the file upload component; As for the second case, we also have to pay a lot of  
"Silver". Unless you have your own web hosting, you can on the server, and
Install the components that you need, which is not possible for most people. Then I  
There's no way to do that? Oh, the answer is yes (of course yes, otherwise I can not  
Write this article AH). Let's do this by using pure ASP code to upload  
and the ability to save to a database (by the way, to display the pictures in the database to the Web page. Span class= "Apple-converted-space" > 
Can).  
First, let's familiarize ourselves with the object methods that will be used. We used to get the previous page to pass
The data that is passed is generally used with the request object. Similarly, we can also use the Request object
To get the uploaded file data, the method used is Request.BinaryRead (). And we're going to go from
The way the data in the database is read from the image is shown on the page:
Request.binarywrite (). When we get the data of the picture, to save it in the database,
You cannot manipulate the database directly using the INSERT statement, but instead use the
AppendChunk method, similarly, to 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 holds the 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 usage method
Request.TotalBytes The amount of data received.
* 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 Syntax
Appends data to a large text, binary data Field, or Parameter object.
Object. AppendChunk Data
Parameters
Object Field or Parameter objects
The data variant type that contains the information appended to the object.
Description
Use the AppendChunk method of the Field or Parameter object to convert long binary or character numbers
To be filled in to the object. In the case of limited system memory, you can use the AppendChunk method for long
Integer values are partially, but not all, operations.
* GetChunk Syntax
Returns the full or partial contents of a large text or binary data Field object.
variable = field. GetChunk (Size)
return value
Returns the Variant type.
Parameters
A Size long expression that is equal to the number of bytes or characters to retrieve.
Description
Use the GetChunk method of the Field object to retrieve part or all of its long binary or character data.
In the case of limited system memory, you can use the GetChunk method to process some, but not all, long integers
Value.
The data returned by the GetChunk call is assigned to "variable". If Size is greater than the remaining data, the
GetChunk only returns the remaining data without padding the "variable" with whitespace. If the field is empty, the
The GetChunk method returns Null. 
Each subsequent GetChunk call retrieves the number of GetChunk  
. However, if you retrieve data from one field and then set or read another field in the current record  
, ADO will assume that the data has been retrieved from the first field. If you 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  
begins reading. If the other Recordset object is not a copy of the first Recordset object,  
Accessing the fields does not break the GetChunk operation.  
If the Attributes bit in the adFldLong property of the Field object is set to True,  
To use the GetChunk method for the field.  
If there is no current record when using the Getchunk method on a Field object, an error is generated 3021 
(no current record).  
ActualSize property, which is the length of the field content
Next, we are going to design our database, as a test of our database structure such as
Under (ACCESS97):
Field Name Type description
ID AutoNumber primary Key value
IMG OLE objects are used to hold 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 to hold image data
Now we are formally writing our ASP code upload section, first of all, we have a
The user's upload interface allows the user 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 part of the black italic in the code must have this attribute in the form, otherwise, there will be no
method to get the data uploaded.
Next, we want to make the necessary data from the browser in the process.asp
Because the data we get in process.asp not only contains the upload we want.
The image of the data, also contains other useless information, we need to eliminate redundant data, and will be at
The picture data saved to the database, here we take Access97 as an example. Specific generation
The 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)
Set Conngraph=server. CreateObject ("Adodb.connection")
Conngraph.connectionstring= "Driver={microsoftAccessDriver (*.mdb)};D bq= "&
Server. MapPath ("Images.mdb") & "; uid=; pwd=; "
Conngraph.open
Set Rec=server.createobject ("Adodb.recordset")
Rec. Open "SELECT * from [images] where ID is null", conngraph,1,3
Rec.addnew
REC ("img"). AppendChunk MyData
Rec.update
Rec.close
Set rec=nothing
Set conngraph=nothing
%>
Okay, so we're going to save the picture from the last one in the database named Images.mdb.
, the rest of the job is to display the image data in the database on the page. Typically in HT
ML, the display image is used label, that is
Is our picture is saved in the database, "picture path" what is it? Oh, actually this
The SRC attribute can also be used in addition to the specified path:
So all we have to do is read out the database in the showimg.asp and meet the criteria.
Data and return it to the SRC attribute, the code is as follows (showimg.asp):
<%
Set Conngraph=server. CreateObject ("Adodb.connection")
conngraph.connectionstring= "Driver={microsoft Access driver (*.mdb)};D bq=" &
Server. MapPath ("Images.mdb") & "; uid=; pwd=; "
Conngraph.open
Set Rec=server.createobject ("Adodb.recordset")
Strsql= "select img from images where id=" & Trim (Request ("id"))
Rec.open strsql,conngraph,1,1
Response.ContentType = "image/*"
Response.BinaryWrite Rec ("img"). GetChunk (REC ("img"). ActualSize)
Rec.close
Set rec=nothing&NBSP;
Set Conngraph=nothing &NBSP
%>&NBSP;
Note Be sure to specify Response.ContentType = "before outputting to the browser" image/* ", &NBSP;
To display the picture normally. &NBSP;
The last point to note is that the processing in my process.asp does not take into account the first page of &NBSP;
(Upload.htm) also has other data, such as <input TYPE=TESXT name=userid> and so on, if &NBSP;
with these items, your process.asp should be careful to dispose of unnecessary data. &NBSP;
What, actually uploading the picture and saving it to the database is easy, so you don't have to &NBSP;
Space cannot use various types of upload components to worry about it. What are you waiting for? Give it a try. &NBSP;
(All programs are in the WinNT4.0 English version, Iis4,access97/ms SQL Server7.0 Span class= "Apple-converted-space" >&NBSP;
Line through)
Upload images with pure ASP code and store them in the database