WEB打印设置解决方案四(在ASP中实现网络打印功能)

使用到的技术:
ASP,WSH,VBScript
文件ASPPrint.asp代码如下:

1@ Language=VBScript 
 1   
 2Option Explicit 
 3
 4Dim strSubmit ' Form中用来保存提交按钮的值   
 5Dim strPrinterPath ' Form中保存网络打印机路径的值   
 6Dim strUsername ' Form中用户名的值   
 7Dim strPassword ' Form中密码的值   
 8Dim strMessage ' Form打印内容的值   
 9Dim objFS ' VBScript中的文件系统对象   
10Dim objWSHNet ' WSH中的网络对象   
11Dim objPrinter ' 打印对象   
12  
13strSubmit = Request.Form("Submit")   
1<html>
2<head>
3<meta content="Microsoft Visual Studio 6.0" name="GENERATOR"/>
4</head>
5<body>   
6  

If strSubmit = "" Then

 1
 2注意的是:   
 3由于我是演示起见,其中有关NT的帐号和密码都是使用了不加密的手段在ASP中传递的真正的运用中应该对该登录过程进行安全处理。   
 4<form action="ASPPrint.asp" id="form" method="POST" name="form">
 5<table align="center" border="0" cellpadding="1" cellspacing="1" width="100%">
 6<tr>
 7<td align="right" nowrap="">网络打印机路径:</td>
 8<td align="left" nowrap=""><input id="printerpath" name="printerpath" type="text" value="\\\&lt; Domain &gt;\&lt; Printer &gt;"/></td>
 9</tr>
10<tr>
11<td align="right" nowrap="">登录帐号:</td>
12<td align="left" nowrap=""><input id="username" name="username" type="text" value="```
13 = strUsername 
14```"/></td>
15</tr>
16<tr>
17<td align="right" nowrap="">登录口令:</td>
18<td align="left" nowrap=""><input id="password" name="password" type="password"/></td>
19</tr>
20<tr>
21<td align="right" nowrap="">请输入你想打印的文字:</td>
22<td align="left" nowrap=""><textarea cols="20" id="message" name="message" rows="2"></textarea></td>
23</tr>
24<tr>
25<td align="right" nowrap=""> </td>
26<td align="left" nowrap=""><input id="submit" name="submit" type="submit" value="Submit"/></td>
27</tr>
28</table>
29</form>
30
31当以上信息被提交后,就可以按照下面的代码进行打印了。   

Else
' 从form中取得响应信息。
strPrinterPath = Request.Form("printerpath")
strUsername = Request.Form("username")
strPassword = Request.Form("password")
strMessage = Request.Form("message")

We will now use the VBScript FileSystemObject object and the WSH Net work object. The Network object will
give us the methods we need to open a printer connection, and the FileSystemObject will allow us to stream our
output to the printer. We create these objects in the following code

example:
' 使用WSH连接网络打印机
Set objWSHNet = CreateObject("WScript.Network")
objWSHNet.AddPrinterConnection "LPT1", strPrinterPath, False, strUsername, strPassword
'使用文件系统对象将打印设备作为一个文件使用
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objPrinter = objFS.CreateTextFile("LPT1:", True)
' 给打印设备送出文本
objPrinter.Write(strMessage)
'关闭打印设备对象并进行错误陷阱处理
On Error Resume Next
objPrinter.Close
' 如果发生错误,关闭打印连接,并输出错误信息
If Err Then
Response.Write ("Error # " &amp; CStr(Err.Number) &amp; " " &amp; Err.Description)
Err.Clear
Else
' 操作成功,输出确认信息
Response.Write("<center>")
Response.Write("<table align="center" border="0" cellpadding="1" cellspacing="1" width="100%">")
Response.Write("<tr><td align="RIGHT"><b>打印消息送出:</b></td>")
Response.Write("<td align="LEFT">" &amp; strMessage &amp; "</td></tr>")
Response.Write("<tr><td align="RIGHT"><b>网络打印机路径:</b></td>")
Response.Write("<td align="LEFT">" &amp; strPrinterPath &amp; "</td></tr>")
Response.Write("<tr><td align="RIGHT"><b>登录帐号:</b></td>")
Response.Write("<td align="LEFT">" &amp; strUsername &amp; "</td></tr>")
Response.Write("</table>")
Response.Write("</center>")
End If
' 取消打印连接
objWSHNet.RemovePrinterConnection "LPT1:"
Set objWSHNet = Nothing
Set objFS = Nothing
Set objPrinter = Nothing
End If

1</body>
2</html>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus