- 注册时间
- 2006-6-3
- 最后登录
- 2007-12-11
- 阅读权限
- 20
- 积分
- 59
- 精华
- 0
- 帖子
- 59

- 性别
- 保密
- 听众数
- 0
- 买家信用
 - 卖家信用
 - 在线时间
- 0 小时
- 相册
- 0
|
|
[vscript]<%@ Language=VBScript %><br><%<br>Option Explicit<br><br>Dim strSubmit @#Form中用来保存提交按钮的值<br>Dim strPrinterPath @#Form中保存网络打印机路径的值<br>Dim strUsername @#Form中用户名的值<br>Dim strPassword @#Form中密码的值<br>Dim strMessage @#Form打印内容的值<br>Dim objFS @#VBScript中的文件系统对象<br>Dim objWSHNet @#WSH中的网络对象<br>Dim objPrinter @#打印对象<br><br>strSubmit = Request.Form("Submit")<br>%><br><br><HTML><br><HEAD><br><META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"><br></HEAD><br><BODY><br><br><%<br>If strSubmit = "" Then<br>%><br><br>注意的是:<br>由于这是演示,其中有关NT的帐号和密码都是使用了不加密的手段在ASP中传递的<br>真正的运用中应该对该登录过程进行安全处理。<br><FORM action="ASPPrint.asp" method=POST id=form name=form><br><TABLE WIDTH=100% ALIGN=center BORDER=0 CELLSPACING=1 CELLPADDING=1><br><TR><br><TD ALIGN=right NOWRAP>网络打印机路径:</TD><br><TD ALIGN=left NOWRAP><INPUT type="text" id=printerpath name=printerpath <br>value="\\< Domain >\< Printer >"></TD><br></TR><br><TR><br><TD ALIGN=right NOWRAP>登录帐号:</TD><br><TD ALIGN=left NOWRAP><INPUT type="text" id=username name=username <br>value="<% = strUsername %>"></TD><br></TR><br><TR><br><TD ALIGN=right NOWRAP>登录口令:</TD><br><TD ALIGN=left NOWRAP><INPUT type="password" id=password <br>name=password></TD><br></TR><br><TR><br><TD ALIGN=right NOWRAP>请输入你想打印的文字:</TD><br><TD ALIGN=left NOWRAP><TEXTAREA rows=2 cols=20 id=message <br>name=message></TEXTAREA></TD><br></TR><br><TR><br><TD ALIGN=right NOWRAP> </TD><br><TD ALIGN=left NOWRAP><INPUT type="submit" value="Submit" <br>id=submit name=submit></TD><br></TR><br></TABLE><br></FORM><br><br>当以上信息被提交后,就可以按照下面的代码进行打印了。<br><%<br>Else<br>@# 从form中取得响应信息。<br>strPrinterPath = Request.Form("printerpath")<br>strUsername = Request.Form("username")<br>strPassword = Request.Form("password")<br>strMessage = Request.Form("message")<br><br>We will now use the VBScript FileSystemObject object and the WSH Network object. The Network object will<br>give us the methods we need to open a printer connection, and the FileSystemObject will allow us to stream our<br>output to the printer. We create these objects in the following code example: <br><br>Set objFS = CreateObject("Scripting.FileSystemObject")<br>Set objWSHNet = CreateObject("WScript.Network")<br>@# 使用WSH连接网络打印机<br>objWSHNet.AddPrinterConnection "LPT1", strPrinterPath, False, strUsername, strPassword<br>@# 使用文件系统对象将打印设备作为一个文件使用<br>Set objPrinter = objFS.CreateTextFile("LPT1:", True)<br>@# 给打印设备送出文本<br>objPrinter.Write(strMessage)<br>@#关闭打印设备对象并进行错误陷阱处理<br>On Error Resume Next<br>objPrinter.Close<br>@# 如果发生错误,关闭打印连接,并输出错误信息<br>If Err Then<br>Response.Write ("Error # " & CStr(Err.Number) & " " & Err.Description)<br>Err.Clear <br>Else<br>@# 操作成功,输出确认信息<br>Response.Write("<CENTER>")<br>Response.Write("<TABLE WIDTH=100% ALIGN=center BORDER=0 CELLSPACING=1 CELLPADDING=1>")<br>Response.Write("<TR><TD ALIGN=RIGHT><B>打印消息送出:</B></TD>")<br>Response.Write("<TD ALIGN=LEFT>" & strMessage & "</TD></TR>")<br>Response.Write("<TR><TD ALIGN=RIGHT><B>网络打印机路径:</B></TD>")<br>Response.Write("<TD ALIGN=LEFT>" & strPrinterPath & "</TD></TR>")<br>Response.Write("<TR><TD ALIGN=RIGHT><B>登录帐号:</B></TD>")<br>Response.Write("<TD ALIGN=LEFT>" & strUsername & "</TD></TR>")<br>Response.Write("</TABLE>")<br>Response.Write("</CENTER>")<br>End If<br>@# 取消打印连接<br>objWSHNet.RemovePrinterConnection "LPT1:"<br>Set objWSHNet = Nothing<br>Set objFS = Nothing<br>Set objPrinter = Nothing<br>End If<br>%><br></BODY><br></HTML> <br>[/vscript]<br>ASP中实现网络打印功能 |
|