How to upload images to the database using ASP

Source: Internet
Author: User
Tags file upload client
Upload | data | database ASP (Active Server Pages) is a very early Web application solution from Microsoft and a simpler programming environment that most developers of web sites are familiar with. With ASP, we can create powerful dynamic Web applications. Although the ASP is very powerful, but some of the functionality with pure ASP code can not be done, in order to ensure the development of more powerful Web applications, we can invoke the use of COM components.

In daily work, such as the development of a "commodity online marketing System" bar, in order to enable customers to understand the appearance of goods, that is, customers look at the text of the product at the same time in the text next to the image of the product to explain, so that customers can have a systematic understanding of the product, the promotion of goods has a great help. So when we develop the system, of course we need to add a picture processing module, how to upload pictures to the server (the picture can be placed in a folder on the Web server can also be placed in a SQL Server server) and how to let the uploaded pictures on the browser side display, this is a developer to consider the problem.

Upload pictures to the server there are several ways, you can use the file upload components can also use the pure ASP code to achieve. In the CSDN Web version of ASP often asked such a question "How to use ASP to upload the picture to the database", in order to facilitate the csdn need to understand this knowledge, this article will explain the use of ASP upload pictures of the practice and give the code, readers.

First look at the various objects and their syntax that are used in your program:

1) Request.BinaryRead () method

Use the Request.BinaryRead () method to get the submitted file data

Syntax

Varrevalue= Request.BinaryRead (number)

The variable Varrevalue return value holds the binary data read from the client;

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

2) Response.BinaryWrite () method

Use the Response.BinaryWrite () method to get picture data from the database and display it in the client's browser.

Syntax

Response.BinaryWrite data

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

3) AppendChunk method

The purpose of the AppendChunk method is to append binary data to a field or Parameter object.

Syntax

Object. AppendChunk data

Parameter data is the packet to append to the field or Parameter object.

4) GetChunk Method

The GetChunk method returns the contents of the binary data.

Syntax

Object. GetChunk (size)

The parameter size indicates the length of the binary data to be returned, which 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, which corresponds to the numbers mentioned above, and can be greater than or equal to the numbers value.

Syntax

Number= Request.TotalBytes

With a general understanding of some of the methods and how they are used, we then start designing the database and related code.

The first step: Database design (for example, Ms SQL Server7):

CREATE TABLE img--Creates a table for storing pictures, named img

(

ID int identity (1,1) not NULL,

IMG Image

)


Second Step: program writing, which omits the user input interface, here only gives very important two files namely Picture upload processing (processimg.asp) and display picture (showimg.asp) file.

1) processimg.asp File code:

〈%

Response.buffer=true

Imagesize=request.totalbytes ' Gets the total number of bytes submitted for the amount of data

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

' Optimized binary data read

BNCRLF=CHRB (&CHR) (10)

Divider=leftb (IMAGEDATA,CLNG (InstrB (ImageData, Bncrlf))-1)

DSTART=INSTRB (ImageData, bncrlf& Bncrlf) +4

DEND=INSTRB (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 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" Here the ID 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

%〉

At this point, this article on how to use ASP upload pictures of the principle and examples are finished, there is nothing wrong to please correct me, thank you! At the same time hope that this article can really help the users need to provide substantive help.



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.