WEB打印設(shè)置處理方案4(在ASP中完成網(wǎng)絡(luò)打印技巧)
發(fā)表時(shí)間:2024-06-08 來源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]使用到的技術(shù): ASP,WSH,VBScript 文件ASPPrint.asp代碼如下: <%@ Language=VBScript %> <% Option Explicit Dim strSubmit ...
使用到的技術(shù):
ASP,WSH,VBScript
文件ASPPrint.asp代碼如下:
<%@ Language=VBScript %>
<%
Option Explicit
Dim strSubmit ' Form中用來保存提交按鈕的值
Dim strPrinterPath ' Form中保存網(wǎng)絡(luò)打印機(jī)路徑的值
Dim strUsername ' Form中用戶名的值
Dim strPassword ' Form中密碼的值
Dim strMessage ' Form打印內(nèi)容的值
Dim objFS ' VBScript中的文件系統(tǒng)對(duì)象
Dim objWSHNet ' WSH中的網(wǎng)絡(luò)對(duì)象
Dim objPrinter ' 打印對(duì)象
strSubmit = Request.Form("Submit")
%>
<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
</HEAD>
<BODY>
<%
If strSubmit = "" Then
%>
注意的是:
由于我是演示起見,其中有關(guān)NT的帳號(hào)和密碼都是使用了不加密的手段在ASP中傳遞的真正的運(yùn)用中應(yīng)該對(duì)該登錄過程進(jìn)行安全處理。
<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>網(wǎng)絡(luò)打印機(jī)路徑:</TD>
<TD ALIGN=left NOWRAP><INPUT type="text" id=printerpath name=printerpath value="\\< Domain >\< Printer >"></TD>
</TR>
<TR>
<TD ALIGN=right NOWRAP>登錄帳號(hào):</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>請(qǐng)輸入你想打印的文字:</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>
當(dāng)以上信息被提交后,就可以按照下面的代碼進(jìn)行打印了。
<%
Else
' 從form中取得響應(yīng)信息。
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連接網(wǎng)絡(luò)打印機(jī)
Set objWSHNet = CreateObject("WScript.Network")
objWSHNet.AddPrinterConnection "LPT1", strPrinterPath, False, strUsername, strPassword
'使用文件系統(tǒng)對(duì)象將打印設(shè)備作為一個(gè)文件使用
Set objFS = CreateObject("Scripting.FileSystemObject")
Set objPrinter = objFS.CreateTextFile("LPT1:", True)
' 給打印設(shè)備送出文本
objPrinter.Write(strMessage)
'關(guān)閉打印設(shè)備對(duì)象并進(jìn)行錯(cuò)誤陷阱處理
On Error Resume Next
objPrinter.Close
' 如果發(fā)生錯(cuò)誤,關(guān)閉打印連接,并輸出錯(cuò)誤信息
If Err Then
Response.Write ("Error # " & CStr(Err.Number) & " " & Err.Description)
Err.Clear
Else
' 操作成功,輸出確認(rèn)信息
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>網(wǎng)絡(luò)打印機(jī)路徑:</B></TD>")
Response.Write("<TD ALIGN=LEFT>" & strPrinterPath & "</TD></TR>")
Response.Write("<TR><TD ALIGN=RIGHT><B>登錄帳號(hào):</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>