如何使用asp进行打印操作
使用到的技术:
asp,wsh,vbscript
文件aspprint.asp代码如下:
<%@ language=vbscript %>
<%
option explicit
dim strsubmit form中用来保存提交按钮的值
dim strprinterpath form中保存网络打印机路径的值
dim strusername form中用户名的值
dim strpassword form中密码的值
dim strmessage form打印内容的值
dim objfs vbscript中的文件系统对象
dim objwshnet wsh中的网络对象
dim objprinter 打印对象
strsubmit = request.form("submit")
%>
<html>
<head>
<meta name="generator" content="microsoft visual studio 6.0">
</head>
<body>
<%
if strsubmit = "" then
%>
注意的是:
由于我是演示起见,其中有关nt的帐号和密码都是使用了不加密的手段在asp中传递的
真正的运用中应该对该登录过程进行安全处理。
<form action="aspprint.asp" method=post id=form name=form>
<table width=100% align=center border=0 cellspacing=1 cellpadding=1>
<tr>
<td align=right nowrap>网络打印机路径:</td>
<td align=left nowrap><input type="text" id=printerpath name=printerpath
value="\\< domain >\< printer >"></td>
</tr>
<tr>
<td align=right nowrap>登录帐号:</td>
<td align=left nowrap><input type="text" id=username name=username
value="<% = strusername %>"></td>
</tr>
<tr>
<td align=right nowrap>登录口令:</td>
<td align=left nowrap><input type="password" id=password
name=password></td>
</tr>
<tr>
<td align=right nowrap>请输入你想打印的文字:</td>
<td align=left nowrap><textarea rows=2 cols=20 id=message
name=message></textarea></td>
</tr>
<tr>
<td align=right nowrap> </td>
<td align=left nowrap><input type="submit" value="submit"
id=submit name=submit></td>
</tr>
</table>
</form>
当以上信息被提交后,就可以按照下面的代码进行打印了。
<%
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 network 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:
set objfs = createobject("scripting.filesystemobject")
set objwshnet = createobject("wscript.network")
使用wsh连接网络打印机
objwshnet.addprinterconnection "lpt1", strprinterpath, false, strusername, strpassword
使用文件系统对象将打印设备作为一个文件使用
set objprinter = objfs.createtextfile("lpt1:", true)
给打印设备送出文本
objprinter.write(strmessage)
关闭打印设备对象并进行错误陷阱处理
on error resume next
objprinter.close
如果发生错误,关闭打印连接,并输出错误信息
if err then
response.write ("error # " & cstr(err.number) & " " & err.description)
err.clear
else
操作成功,输出确认信息
response.write("<center>")
response.write("<table width=100% align=center border=0 cellspacing=1 cellpadding=1>")
response.write("<tr><td align=right><b>打印消息送出:</b></td>")
response.write("<td align=left>" & strmessage & "</td></tr>")
response.write("<tr><td align=right><b>网络打印机路径:</b></td>")
response.write("<td align=left>" & strprinterpath & "</td></tr>")
response.write("<tr><td align=right><b>登录帐号:</b></td>")
response.write("<td align=left>" & strusername & "</td></tr>")
response.write("</table>")
response.write("</center>")
end if
取消打印连接
objwshnet.removeprinterconnection "lpt1:"
set objwshnet = nothing
set objfs = nothing
set objprinter = nothing
end if
%>
</body>
</html>
