Using pure ASP code to realize picture uploading

Source: Internet
Author: User
Tags contains count file upload integer client
Upload Source Author: herd

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 amount of data obtained using the method request.totalbytes.
*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 to perform partial rather than complete operations on long values.
*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 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 (access2000):



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-provided upload interface, you can let users choose to upload pictures. The code is as follows
(upload.htm):
<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>
Note enctype= "Multipart/form-data", must have this attribute in the form, otherwise, will not be uploaded data. 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 access2000 as an example. The specific code is as follows (process.asp):
<%
Response.buffer=true
Formsize=request.totalbytes
Formdata=request.binaryread (Formsize)
BNCRLF=CHRB (&AMP;CHRB) (10)
Divider=leftb (FORMDATA,CLNG (INSTRB (FORMDATA,BNCRLF))-1)
DATASTART=INSTRB (FORMDATA,BNCRLF&AMP;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. 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
%>
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,
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") &; uid=; pwd=; "
Conngraph.open
Setrec=server.createobject ("Adodb.recordset")
Strsql= "selectimgfromimageswhereid=" &trim (Request ("id")
Rec.



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.