Upload and store images in a database with pure ASP code

Source: Internet
Author: User

Using ASP to write website application time is long, inevitably encounter a variety of problems, which about 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 provides the "one 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 use of the function is very powerful, but because in many cases, we can only use free space to support ASP or to rent someone else's virtual space, for the first case, we have no possibility to use the File Upload component As for the second situation, we also have to pay a lot of "silver" to be able. Unless you have your own virtual hosting, you can easily install the components you need on the server, which is not possible for most people. So there's no way we can do that? Oh, the answer is yes (sure, otherwise I can not write this article AH). Let's take a look at the use of pure ASP code to achieve image upload and save to the database function (by the way also to display the image in the database to the function of the page).
First, let's familiarize ourselves with the object methods that will be used. The data we use to get the previous page is typically used with the request object. Similarly, we can use the request object to get the uploaded file data, using the Request.BinaryRead () method. And we're going to read the data from the database. The method used to display the image to the Web page is:
Request.binarywrite (). When we get the image data, to save to the database, you can not directly use the INSERT statement to the database operation, but to use the AppendChunk method of ADO, the same, read out the database image data, 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 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
Append data to large text, binary data Field, or Parameter pair?
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 fill in the long binary or character data into an object. In the case of limited system memory, you can use the AppendChunk method to perform some, but not all, operations on long values.
* 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 handle some, but not all, long integer values.
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 data that began at the time the GetChunk call stopped. 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 you call the GetChunk method again on the first field, ADO interprets the call as a new GetChunk operation and starts reading from 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 the Field object is set to True, you can use the GetChunk method for that field. If there is no current record when using the Getchunk method on a Field object, error 3021 will be generated (no current record). Next, we are going to design our database as a test of our database structure as follows (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 began to formally write our pure ASP code upload part, first of all, we have a user-provided upload interface, can let users choose to upload images. The code is as follows
(upload.htm):
$#@60;html$#@62;
$#@60;body$#@62;
$#@60;center$#@62;
$#@60;form name= "mainform" enctype= "Multipart/form-data"
action= "process.asp" method=post$#@62;
$#@60;input Type=file name=mefile$#@62;$#@60;br$#@62;
$#@60;input type=submit name=ok value= "OK" $#@62;
$#@60;/form$#@62;
$#@60;/center$#@62;
$#@60;/body$#@62;
$#@60;/html$#@62;
Note that the black italic part of the code must have this attribute in the form, otherwise it will not be able to get the data uploaded.
Next We want to do the necessary processing of the data obtained from the browser in process.asp, because the data we get in process.asp not only contains the data we want to upload, but also contains other useless information, we need to eliminate redundant data and save the processed picture data to the database. , here we take Access97 as an example. The specific code is as follows (process.asp):
$#@60;%
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
%$#@62;
OK, so we've saved the images from the database named Images.mdb, and the rest of the work is to display the image data from the database on the page. Generally in HTML, the display image is used $#@60;img$#@62, the label, that is, $#@60;img src= "picture path" $#@62, but our picture is saved to the database, "picture path" what is it? Oh, in fact, this SRC attribute in addition to the specified path, you can also use this:
$#@60;img src= "Showimg.asp?id=xxx" $#@62;
So all we have to do is read the data from the database in showimg.asp and return it to the SRC attribute, and the code is as follows (showimg.asp):
$#@60;%
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
%$#@62;
Note Be sure to specify Response.ContentType = "image/*" before outputting to the browser so that the picture is displayed normally.
The last point to note is that the processing in my process.asp does not take into account the other data in the first page (upload.htm), such as $#@60;input TYPE=TESXT name=userid$#@62, and so on, if there are these items, Your process.asp should be careful to dispose of unnecessary data. How, actually upload the picture and save to the database is very simple, so no longer for their own space can not use all kinds of upload components worry about it. What are you waiting for? Give it a try.
(all the above programs are in the WinNT4.0 English version, Iis4,access97/ms SQL Server7.0 run through the Shanghai Treatment Impotence Hospital editor finishing)

Upload and store images in a database with pure ASP code

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.