Use the FileSystem component to implement specific local printing in WEB Applications

Source: Internet
Author: User

1. Introduction

With the rapid development of the Internet, many enterprises have developed WEB-based business application systems. Generally, WEB-based business applications adopt a structure of three or more layers. The front-end client is a common WEB browser, and the application layer of intermediate business logic is stored on the WEB server, the service component on the WEB server accesses the background database. To connect the business system to the Internet, WEB servers and database systems are hosted in IDCs (Internet data centers). Therefore, business data needs to be extracted from servers hosted in IDCs and printed and output locally. When the client browser can only print simple HTML pages, more powerful and flexible printing and output functions are required. The browser functions are extended by using related technologies, more complex data printing tasks, such as receiving and receipt. I used the FileSystem component to develop the education management software for an online education company to print the triple receipt credential and admission ticket.

2. Introduction to FileSystem Components

The FileSystem component is actually an ActiveX control, which is stored on the WINDOWS platform (both Win98, Win2000, and NT environments contain this component .), CLSID: 0D43FE01-11CF-8940-00A0C9054228. Familiar with ASP programmers may have used FileSystem components. Generally, this component is used on the server side to access and control text files, folders, and drives. You can create an object instance of the FileSystem component in two ways:

1. Use the SERVER. CreateObject method of ASP's built-in Object Server to create an instance of the SERVER object.

2. Use the Vbscript script function CreateObject to create an instance of the client object (this method can also be used on the server side, but sometimes an error occurs, which is generally not used ;).

The FileSystem component performs the following operations on text files:

CreateTextFile (Filename, [Overwrite if exists], [Unicode/ASCII]) // create a text file

OpenTextFile (Filename, [Input/output mode], [Create if not exists], [Format]) // open a text file

CopyFile (Filename1, Filename2, [Overwrite]) // copy a text file

MoveFile (Filename1, Filename2) // move a text file

DeleteFile (Filename) // delete a text file

GetFile (Filename) // obtain a text file

FileExists (Filename) // determines whether a text file exists

The FileSystem component also has methods similar to text files for folders and drives, which are not described here.

In fact, the FileSystem component can also be used on the client. The second method of creating the FileSystem component object instance can be used to access and control the text files, folders, and drives of the client system. Because the Microsoft platform has the FileSystem component, clients on the Microsoft platform do not need to be downloaded from a remote server. They will install and register the operating system on their own; for clients on other platforms, you must install the plug-in and download the FileSystem component.

3. Implementation Mechanism of specific local printing

The FileSystem component is used to implement the specific printing process on the local end:



1. The client sends data requests to the WEB server;

2. The WEB server interacts with the background database based on the business processing logic, obtains the required data to form an HTML page, and carries the VBScript containing the FileSystem component object to send the HTML page back to the client browser;

3. Run the script on the client to obtain the HTML page component data, establish a connection with the printer, and print data to the printer.

There is no difference between the first two steps and general WEB applications. It must be noted that the third step is to use the Vbscript function CreateObject to create an instance of the FileSystem component object; call the CreateTextFile method of the instance to create a text file, set the print port LPT1 or LPT2 as the file name parameter, and set the file parameter that can be overwritten to TRUE to establish a connection with the printer; call the WriteLine method of the obtained printer text file stream instance to output the printed content to the printer.

4. program example

Client script:

<Script Language = VBScript>

Function print_onclick // print the function

Dim label

Label = document. printinfo. label. value // obtain HTML page data

Set objfs = CreateObject ("Scripting. FileSystemObject") // create an instance of the FileSystem Component Object

Set objprinter = objfs. CreateTextFile ("LPT1:", true) // establish a connection with the printer

Objprinter. Writeline ("__________________________________") // output printed content

Objprinter. Writeline ("| ")

Objprinter. Writeline ("| the data you printed is:" & label & "| ")

Objprinter. Writeline ("| ")

Objprinter. Writeline ("| _________________________________ | ")

Objprinter. close // disconnect from the printer

Set objprinter = nothing

Set objfs = nothing // disable the FileSystem Component Object

End function

</Script>

Server script:

<% .........

Set conn = server. CreateObject ("adodb. connection ")

Conn. Open "DSN = name; UID = XXXX; PWD = XXXX ;"

Set rs = server. CreateObject ("adodb. recordset ")

Rs. Open ("select ......"), Conn, 1, 1

.......... %> // Interact with the database

HTML page encoding:

<HTML>

.........

<Form id = printinfo NAME = "printinfo">

<INPUT type = "button" value = "print>" id = print name = print> // call the print function

<INPUT type = hidden id = text1 name = label value = <% = ......... %> // Save data sent from the server

.........

</HTML>

5. Description and conclusion

1. Before using this method to print a task, you must adjust the security settings of the browser; reduce the security level on Internet Options and fully trust your site, attackers can execute scripts in a browser to access local resources.

2. This method can print remote server data on a LAN printer, you only need to use the local port registered with the printer on the LAN to replace the local printer port as a parameter for the CreateTextFile method for creating a text file.

3. This method allows you to select the width, height, quantity, and content for printing and print them repeatedly.

4. You do not need to write ActiveX controls to complete specific printing functions.

5. The disadvantage is that you need to adjust the browser's security settings and do not support printing images and special fonts.

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.