Author: Zhui Feng
has been using ASP to write website applications for a long time. It is inevitable that you will encounter various problems. Among them, how to upload files to the server is probably the most common problem, especially uploading pictures. For example, if you want to To implement a "one star per day" function similar to that provided by NetEase virtual community in your own community, you must provide the function for netizens to upload photos. To upload image files to the server, you can use various free file upload components. Although they are very powerful, in many cases, we can only use free ASP-supported space or rent other people's virtual space. For the first case , it is simply impossible for us to use the file upload component; as for the second case, we also have to pay a lot of "money". Unless you have your own virtual host, you can easily install the components you need on the server. This situation is out of reach for most people. Then there is nothing we can do? Haha, the answer is yes (of course it is yes, otherwise I wouldn’t be able to write this article). Let's use pure ASP code to implement the function of uploading images and saving them to the database (by the way, we also implement the function of displaying images in the database on the web page).
First, let's get familiar with the object methods we will use. We generally use the Request object to obtain the data passed from the previous page. Similarly, we can also use the Request object to obtain the uploaded file data. The method used is Request.BinaryRead(). The method we use to read the image data from the database and display it on the web page is:
Request.BinaryWrite(). When we get the image data and save it to the database, we cannot directly use the Insert statement to operate the database. Instead, we must use the AppendChunk method of ADO. Similarly, to read the image data in the database, we must use GetChunk. method. The specific syntax of each method is as follows:
*Request.BinaryRead syntax:
variant=Request.BinaryRead(count)
parameter
variant
The return value holds the data read from the client.
count
Indicates the amount of data to be read from the client. This value is less than or equal to the amount of data obtained using the Request.TotalBytes method.
*Request.BinaryWrite syntax:
Request.BinaryWritedata
parameter
data
The packet to be written to the client browser.
*Request.TotalBytes syntax:
variant=Request.TotalBytes
parameter
variant
Returns the number of bytes of data read from the client.
*AppendChunk syntax appends data to a large text, binary data Field or Parameter object.
object.AppendChunkData
parameter
objectField or Parameter object
Data variant type, containing data appended to the object.
Description Use the AppendChunk method of the Field or Parameter object to fill in long binary or character data into the object. When the system memory is limited, you can use the AppendChunk method to perform some but not all operations on long integer values.
*GetChunk syntax returns all or part of the contents of a large text or binary data Field object.
variable=field.GetChunk(Size)
The return value returns the variant type.
parameter
Size long integer expression, equal to the number of bytes or characters to be retrieved.
Description Use the Field object's GetChunk method to retrieve some or all of its long binary or character data. When the system memory is limited, you can use the GetChunk method to process some but not all long integer values.
The data returned by the GetChunk call will be assigned to the "variable". If Size is greater than the remaining data, then
GetChunk just returns the remaining data without filling the "variable" with blanks. If the field is empty, then
GetChunk method returns Null.
Each subsequent GetChunk call will retrieve data starting where the previous GetChunk call left off. However, if you retrieve data from one field and then set or read the value of another field in the current record, ADO will assume that the data has been retrieved from the first field. If the GetChunk method is called again on the first field, ADO will interpret the call as a new GetChunk operation and start reading from the beginning of the record. If the other Recordset object is not a copy of the first Recordset object, accessing fields in it will not break the GetChunk operation.
If the adFldLong bit in the Field object's Attributes property is set to True, you can use the GetChunk method on the field.
If there is no current record when using the Getchunk method on a Field object, error 3021 (No Current Record) will be generated.
Next, we are going to design our database. As a test, our database structure is as follows (access2000):
Field name type description id automatic number primary key value
img OLE object is used to save image data
For 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 start to formally write the upload part of our pure ASP code. First, we have an upload interface provided to the user, which allows the user to select the image to upload. 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 the Form, otherwise, the uploaded data will not be obtained. Next, we need to perform necessary processing on the data obtained from the browser in process.asp, because the data we obtain in process.asp not only contains the data of the uploaded pictures we want, but also Contains other useless information, we need to eliminate redundant data and save the processed image data to the database. Here 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(13)&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)};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
%>
Okay, now we have saved the uploaded images into a database named images.mdb. The remaining work is to display the image data in the database on the web page. Generally, in HTML, the <IMG> tag is used to display images, that is, <IMGSRC="image path">, but our images are saved in the database. What is the "image path"? Haha, in fact, in addition to specifying the path, this SRC attribute can also be used like this:
<IMGSRC="showimg.asp?id=xxx">
So, all we have to do is read the qualified data from the database in showimg.asp and return it 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 Response.ContentType="image/*" must be specified before outputting to the browser.
in order to display the image normally.
The last thing to note is that the processing in my process.asp does not take into account that there are other data in the first page (upload.htm), such as <INPUT type=tesxt name=userid>, etc., if there are these items , your process.asp should pay attention to processing unnecessary data.