Image data processing of ASP SQL Server
ASP (Active Server Pages) is a web application solution that Microsoft has been launched earlier, and most of them are a relatively simple programming environment that is familiar with website developers. With ASP we can create a powerful dynamic web application. Although the ASP is very powerful, some functions cannot be completed with a pure ASP code. In order to ensure that it is more powerful for web applications, we can call COM components.
In daily work, for example, a "product online sales system" is developed, in order to make customers understand the appearance of the goods, the customer is introducing the text next to the text in the text, but also the image of the product. Such a customer can have a systematic understanding of the product, which has a great help to sell goods. So we must join the image processing module when developing a system, that is, how to upload the image to the server (the picture can be placed in a folder on a folder on a folder on the SQL Server server) and how to get uploaded pictures The browser is displayed, which is the problem that developers should take into account.
Upload images to the server have multiple methods, you can use the file upload assembly to use pure ASP code to implement. On the 9CBS Web ASP, there are frequent netizens asking such issues "How to use ASP to upload the picture to the database" Out of the code, readers.
First understand the various objects used in the program and their grammar:
1) Request.binaryRead () method
● Use the request.binaryRead () method to get submitted file data
● Syntax
VarRevalue = Request.BinaryRead (Number)
Variable VARREVALUE Return Value Save 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 the picture data from the database and display it to the client's browser.
● Syntax
Response.binaryWrite Data
The parameter DATA is the binary packet to write into the client browser.
3) Appendchunk method
● The action of the appendchunk method is to append binary data to the Field or Parameter object.
● Syntax
Object.Appendchunk Data
The parameter DATA is to add a packet to the field or parameter object.
4) getChunk method
● The getChunk method returns the content of 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
● Request.Totalbytes method Returns the number of bytes of data taken from the client, which corresponds to the Number mentioned above, can be greater than or equal to the Number value.
● Syntax
Number = Request.totalBytes
After a substantially understanding some methods and how it is used, we will start designing the database and related writing code.
Step 1: Database design (as an example of MS SQL Server 7):
CREATE TABLE IMG - Create a table for storing pictures, name IMG
(
ID Int IDENTITY (1, 1) Not NULL,
IMG image
)
Step 2: The program is written, which omitted the user input interface, which gives only two important two files, which are image upload processing (ProcessImg.asp) and display pictures (Showimg.asp) files. 1) Processimg.asp file code:
<%
Response.buffer = TRUE
Imagesize = request.totalbytes' Number of total bytes of submitted data volume
ImageData = Request.binaryRead (ImageSize) Saves data from the client read
'Optimize read binary data
BNCRLF = Chrb (13) & chr (10)
Divider = Leftb (ImageData, ClNG (IMAGEDATA, BNCRLF) - 1)
DStart = INSTRB (ImageData, Bncrlf & Bncrlf) 4
Dend = INSTRB (DSTART 1, ImageData, Divider) - DSTART
MyData = MIDB (imagedata, dstart, dend)
'Creating an object instance
Set imgconn = server.createObject ("adoDb.connection")
StrConn = "driver = {SQL Server}; server = servername;" & _
"UID = xxxx; pwd = xxxx; database = 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
'Additional data to the database
Rs.addnew
RS ("img"). Appendchunk MyData
Rs.Update
'Close and release the object
Rs.close
IMGCONN.CLOSE
SET RS = Nothing
Set imgconn = Nothing
%>
2) showimg.asp file code:
<%
Response.expires = 0
Response.buffer = TRUE
Response.clear
'Creating an object instance
Set imgconn = server.createObject ("adoDb.connection")
StrConn = "driver = {SQL Server}; server = servername;" & _
"UID = xxxx; pwd = xxxx; database = database = DatabaseName"
IMGCONN.Open Strconn
SET RS = Server.createObject ("AdoDb.Recordset")
SQL = "SELECT IMGWHERE ID = 1" The ID here can be obtained using the Request ("ID")
Rs.open SQL, IMGCONN, 1, 1
Response.contentType = "image / *"
Response.binaryWrite RS. ("IMG"). Getchunk (7500000)
'Close and release the object
Rs.close
IMGCONN.CLOSE
SET RS = Nothing
Set imgconn = Nothing
%>