How to use ASP to upload images to a database

Source: Internet
Author: User

ASP (Active Server Pages) is a web application launched by Microsoft very earlyProgramThe solution is also a simple programming environment that most website developers are familiar. Through ASP, we can create dynamic and powerful web applications. Although ASP is powerful, some functions use pure ASPCodeNo. To ensure the development of more powerful web applications, we can call the COM component.

In daily work, for example, developing an "online product sales system", in order to make the customer understand the appearance of the product, that is to say, when the customer looks at the product text, there is a picture of the product next to the text to explain it, so that the customer can have a systematic understanding of the product, it is of great help to promote products. Therefore, we need to add the image processing module when developing the system, that is, how to upload images to the server (images can be placed in a folder on the Web server or in the SQL Server server) and how to display uploaded images on the browser side are all considerations of developers.

There are multiple ways to upload images to the server. You can use the File Upload Component or the pure ASP code. On the web version of ASP of csdn, some netizens often ask the following question: "How to use ASP to upload images to a database". To facilitate csdn, you need to understand this knowledge.ArticleI will explain in detail how to use ASP to upload images and provide code to help readers.

First, let's take a look at the various objects used in the program and Their syntax:

1) Request. binaryread () method

● The request. binaryread () method can be used to obtain the submitted file data.

● Syntax

Varrevalue = request. binaryread (number)

The Return Value of the variable varrevalue stores the binary data read from the client;

The number parameter indicates the size of the binary data to be read from the client.

2) response. binarywrite () method

● The response. binarywrite () method can be used to obtain image data from the database and display it in the browser of the client.

● Syntax

Response. binarywrite data

The parameter data is a binary data packet to be written into the client browser.

3) AppendChunk method

● The AppendChunk method is used to append binary data to a field or parameter object.

● Syntax

Object. AppendChunk data

The parameter data is the data packet to be appended to the field or parameter object.

4) getchunk Method

● The getchunk method returns the binary data.

● Syntax

Object. getchunk (size)

The size parameter indicates the length of the binary data to be returned. It can be a long integer expression.

5) request. totalbytes Method

● The request. totalbytes method returns the number of bytes of data read from the client. This value corresponds to the number mentioned above and can be greater than or equal to the number value.

● Syntax

Number = request. totalbytes

After learning about some methods and their usage, we will start designing databases and writing code.

Step 1: Database Design (Taking ms SQL Server7 as an example ):

Create Table IMG -- create a table for storing images and name it img

(

Id int identity (1, 1) not null,

IMG Image

)

Step 2: Compile the program. The user input interface is omitted. Here, only two important files are provided: Image Upload processing (processimg. asp) and showimg. asp.

1) processimg. asp file code:

<%

Response. Buffer = true

Imagesize = request. totalbytes 'get the total number of bytes of the submitted data volume

Imagedata = request. binaryread (imagesize) 'stores the data read from the client

'Optimize the binary data read

Bncrlf = chrb (13) & CHR (10)

Divider = leftb (imagedata, clng (imagedata (bncrlf)-1)

Dstart = imagedata (bncrlf & bncrlf) + 4

Dend = partition B (dstart + 1, imagedata, divider)-dstart

Mydata = midb (imagedata, dstart, dend)

'Create an object instance

Set imgconn = server. Createobject ("ADODB. Connection ")

Strconn = "driver = {SQL Server}; server = servername ;"&_

"Uid = xxxx; Pwd = xxxx; database = databasename"

Imgconn. Open strconn

Set rs = server. Createobject ("ADODB. recordset ")

SQL = "select * From IMG where ID is null"

Rs. Open SQL, imgconn, 1, 3

'Append data to the database

Rs. addnew

RS ("IMG"). AppendChunk mydata

Rs. Update

'Close and release objects

Rs. Close

Imgconn. Close

Set rs = nothing

Set imgconn = nothing

%> 〉

2) showimg. asp file code:

<%

Response. expires = 0

Response. Buffer = true

Response. Clear

'Create an object instance

Set imgconn = server. Createobject ("ADODB. Connection ")

Strconn = "driver = {SQL Server}; server = servername ;"&_

"Uid = xxxx; Pwd = xxxx; database = databasename"

Imgconn. Open strconn

Set rs = server. Createobject ("ADODB. recordset ")

SQL = "select IMG from IMG where id = 1" the ID here can be obtained using request ("ID ")

Rs. Open SQL, imgconn, 1, 1

Response. contenttype = "image /*"

Response. binarywrite Rs. ("IMG"). getchunk (7500000)

'Close and release objects

Rs. Close

Imgconn. Close

Set rs = nothing

Set imgconn = nothing

%> 〉

So far, this article has completed the principles and examples of how to use ASP to upload images. please correct me if anything is wrong. Thank you! At the same time, I hope this article can indeed provide substantial help to netizens who need help in this area. (Csdn)

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.