用ASP進行網路列印功能
來源:互聯網
上載者:User
<DIV class=ContentFont id=NewaspContentLabel style="PADDING-RIGHT: 10px; DISPLAY: block; PADDING-LEFT: 10px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px"><%@ 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> </DIV>