Using ASP to write Web applications for a long time, will inevitably encounter a variety of problems, including on 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 similar to the 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 ASP or rent other people's virtual space, for the first case, we simply do not have the possibility 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 a virtual host of your own, you can easily install the components you need on the server, which is impossible for most people. So there's no way we can do that? Oh, the answer is yes (certainly, or 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
Appends data to a large text, binary data Field, or Parameter object.
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):
< html>
< body>
< center>
< form name= "mainform" enctype= "Multipart/form-data"
action= "Process.asp" method=post>
< input Type=file name=mefile>
< input type=submit Name=ok value= "OK" >
</form>
</center>
</body>
Note that the black italic part of the code must have this attribute in the form, otherwise you will not be able to get the data uploaded.
Next We're going to do the necessary processing of the data we get from the browser in process.asp, because the data that we get in the process.asp not only contains the data that we want to upload, but also contains other useless information, we need to eliminate the redundant data and save the processed image data to the database. , here we take Access97 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)
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")
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
% >
OK, so we'll save the pictures from the Images.mdb to the database named "," and the rest of the work is to display the image data from the database to the page. Generally in HTML, display pictures are used tags, that is, but our pictures are saved to the database, "picture path" is what? Oh, in fact, this SRC attribute in addition to the specified path, you can use OH:
< IMG src= "Showimg.asp?id=xxx" >
So all we have to do is read the data from the database in showimg.asp and return it to the SRC attribute, 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 (7500000)
Rec.close
Set rec=nothing
Set conngraph=nothing
% >
Note You must specify Response.ContentType = "image/*" before outputting to the browser to display the picture normally.
The last thing to note is that the processing in my process.asp does not take into account any other data in the first page (upload.htm), such as < INPUT TYPE=TESXT name=userid>, and so on, if there are these items, Your process.asp should be careful to dispose of unnecessary data. How, in fact, upload pictures and save it to the database is very simple, so no longer need for their own space can not use all kinds of upload components to worry about it. What are you waiting for? Try it quickly.
(all of the above programs are in the WinNT4.0 English version, Iis4,access97/ms SQL Server7.0 run through)