Upload and store the picture in the database with the Pure ASP code (II.)

Source: Internet
Author: User
Tags integer
Upload | data | database * 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 (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)



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.