Using ASP to implement web-based database data system

Source: Internet
Author: User
Tags count execution file upload html page iis implement one table microsoft frontpage
web| Data | database


Introduction to ASP
ASP (Active Server page) is a dynamic web development technology based on IIS (Internet Information Server), which is launched by Microsoft Corporation. ASP is based on the principles of the ISAPI (Internet Service API), which typically uses the VBScript language for embedded scripting of HTML files, and the filename suffix is. asp. It works as shown in Figure 1.
When the browser issues an. asp request to the Web server, the Web server starts running the ASP script, and then the Web server launches asp,asp checks the requested file from beginning to end, executing the embedded ASP script in the file. Finally, the execution Results HTML Web page is returned to the Web Server. The Web server then sends the HTML page to the client. This allows users to design ASP scripts based on requests made by the browser, resulting in different script execution results for different requests. ASP has many advantages (such as simple use, high execution efficiency, etc.), but it also has its inherent weakness, that is, it uses scripting language, limited functionality, difficult to complete many complex business functions. To remedy this weakness, Microsoft provides the ability to use ActiveX components in ASP, so many complex features can be implemented in many other advanced languages, such as Visual C + +, and then packaged as ActiveX for ASP calls. ADO (ActiveX Data Object) is a common database access component in ASP.

Key technologies of Web database Data system implementation
In order to realize the Web-based database data system, the key problem is to solve two aspects. The first is to implement the upload of client file data through a browser to the Web server, and save the contents of the file to the database, and secondly, to read the contents of the data from the database and send the content to the client browser. To illustrate how to implement these two technologies, we use a simple database for example (the database environment used here is MS SQL Server 6.5), which has only one table (UploadFile) and is structured as shown in Figure 2.
Where the ID field is the keyword, the type of the self added, the title represents the data title, filename is the uploaded data file name, and the Content field holds the binary data file contents. The following describes the implementation of uploading client file data to a Web server through a browser, saving the contents of the file to a database, and reading the contents of the data from the database and sending the content to the client browser. The Web server platform is Windows NT Server 4.0 + IIS4.0 +sql Server 6.5, where the client uses IE4.0 and system debugging passes.

Uploading to a Web server using ASP implementation files
Uploading files based on the Web is subject to the RFC1867 standard (supported by IE3.0 and Netscape over 3.0 versions of browsers). However, IIS does not provide support for this. Therefore, to implement file uploads using ASP, you must invoke the appropriate ActiveX component, which is not provided by the ASP itself and can be developed either by itself (according to the RFC1867 standard) or by third-party-provided components. The method described here uses the Third-party component ASPUpload.dll, which must be registered on the Web server before use (copy ASPUpload.dll to the \Winnt\System32 directory, then the cloud runs "regsvr32 aspupload.dll ”)。
Where the client source files are as follows (uploadtest.htm):
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title> File Upload Client </title>
<body>
<form method= "POST" action= "uploadtest1.asp" enctype= "Multipart/form-data"
Name= "Uploadform" >
<p><input type= "File" name= "File1" > </p>
<p><input type= "Submit" value= "Submit" Name= "Upload" ></p>
</form>
</body>
Where the client file should pay attention to two points:
* The enctype of the File Upload Submission form (form) must be specified as "Multipart/form-data"
* Statement <input type= "file" name= "File1" > indicates that the file domain is uploaded, and users can enter or select files in that field.
The server-side source files are as follows (uploadtest1.asp):
<%response.buffer=true%>
<%set upload=server.createobject ("Persits.upload.1") ' Create a File Upload component
Count=upload.save ("E:\aspupload") ' Saves the client file to the local hard drive on the Web server side%>

<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<title>upload test</title>
<meta name= "generator" content= "Microsoft FrontPage 3.0" >
<body>
<p> uploaded <%=Count%> files </p>
<p>file has been uploaded.</p>
</body>
where script set Upload=server.createobject ("Persits.upload.1") creates a file upload component that saves the contents of the browser-side file to the specified path by calling the Save method.

Keep files in a database
to save files in a database The main use of the Todatabase method of the file objects in the AspUpload component. The source files are as follows:
Client source file (uploadtodb.htm):
<meta http-equiv= "Content-type" content= "Text/html; charset=gb2312
<title> File upload client </title>
<body>
<form method= " POST "action=" uploadtodb.asp "enctype=" Multipart/form-data "
Name=" Formupload "
<p><input type=" File "Name=" FileUpload > </p>
<p><input type= "Submit" value= "Upload" name= "B1" ></P>
</form>
<p><a href= "readfile.asp" > read file </a></p> in database;
</body>
</ Html>

Server-side source files (uploadtodb.asp):
<meta http-equiv= content-type "content=" text/ html charset=gb2312
<title> file uploaded to database </title>
<body>
<%set upload= Server.CreateObject ("Persits.upload.1")
Count=upload.save ("E:\aspupload")
On Error Resume Next
Set Fileobj=upload.files ("FileUpload")
sql= "insert into UploadFile (title,filename,content) VALUES ('" & fileobj.path& "', '" &FileObj.Path& "",?) "
Response.Write SQL
Fileobj.todatabase "dsn=fileupload; Uid=sa; ", SQL
If err<>0 then
Response.Write" Error Saving the file: "&err.description
Else
Fileobj.delete
Response.Write "Success!"
End If
%>
</body>

Reading the contents of a file from a database and sending it to the client browser
read the content from the database before you send it to the browser, you must first let the browser know the data type of the content, which is implemented by sending a contenttype description to the client. For simplicity, this assumes that the content sent is a Word document and displays the most recently inserted record. The source files are as follows:
The client source file is uploadtodb.htm (the client file in the previous section).
Server-side source files (readfile.asp):
<%response.expires = 0
Response.buffer=true%>
<%response.clear
Response.ContentType = "Application/msword"
Set Conn=server.createobject ("Adodb.connection")
Conn.Open "DSN=" FileUpload; Uid=sa; "
Set Rs1=conn.execute ("Select Maxid=max (ID) from UploadFile")
sql= "SELECT * from UploadFile where id=" &rs1 ("M Axid ")
Set Rs=conn.execute (SQL)
Response.BinaryWrite rs (" Content ")
Rs.close
Rs1.close
Conn.close
Response.End
%>
where the WEB server sends content-type= "Application/msword" to the client that the client considers to be a Word document. The server then reads the contents of the file from the database (for simplicity, assuming the last record in the database) and sends it to the client as a binary stream (invokes the BinaryWrite method of the ASP built-in object response). When the client receives the content, it automatically starts the Word OLE service, which embeds the word control into the formatted display of the content that is received in browser ie.

Summarize
In summary, the key technology of Web database data system can be realized conveniently by using ASP script and File Upload component and ADO component. With the above technology, the simple system of web-based database data can be expanded and realized easily. Based on the above technology, the author develops a set of database archives management system on Internet, which is very stable and reliable (a large part owes to the stability of AspUpload components), and the file contains HTML, Word, Excel, text, image and other formats, File information management, maintenance and retrieval is very convenient, by the user's consistent high praise.




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.