Website Tutorial: Using pure ASP code to upload and store the image in the database

Source: Internet
Author: User

Intermediary transaction http://www.aliyun.com/zixun/aggregation/6858.html ">seo diagnose Taobao guest cloud host technology Hall

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. Similarly, we can use the request object to obtain the uploaded file data, using the method 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)


parameter


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 use method

The amount of data
request.totalbytes.


* Request.binarywrite Syntax:


Request.binarywrite Data


parameter


Data


the packet to be written to the client browser.


* request.totalbytes Syntax:


variant = Request.TotalBytes


parameter


Variant


returns the number of bytes read from the client to the amount of data.


* AppendChunk Grammar


appends data to large text, binary data Field, or Parameter objects.


object. AppendChunk Data


parameter


Object Field or Parameter objects

The
data variant containing the appended to the object.


Description


uses the AppendChunk method of a Field or Parameter object to fill in an object with long binary or character data. With limited system memory, you can use the AppendChunk method to perform partial rather than complete operations on long values.


* GetChunk Grammar


returns all or part of a large text or binary data Field object.


variable = field. GetChunk (Size)


return value


returns variant type.


parameter


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 Size is greater than the remaining data, GetChunk only returns the remaining data without padding the variable with white space. If the field is empty, 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.


You can use the GetChunk method for a Field object if the adFldLong bit in the Attributes property is set to True.


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 AutoNumber primary key value


img OLE object is used 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 formally write our Pure ASP code upload section, first of all, 11545.html "> We have a user-provided upload interface, allows users 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>


<input type=file name=mefile><br>


<input type=submit name=ok value= "OK" >


</form>


</center>


</body>


</html>


Note that the Black italic part of the code, you must have this attribute in the form, otherwise, you 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 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


%>


Well, we have saved the pictures from the last one to the database named Images.mdb, and the rest of the work is to display the image data from the database to the Web page. Generally in HTML, display pictures are used <IMG> tags, that is, <img src= "picture path", 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 the 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 other data in the first page (upload.htm), such as <input TYPE=TESXT name=userid> and so on, if you have these items, Your process.asp should be careful to dispose of unnecessary data.

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.