熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

用ASP進行網絡打印的功能

2013-11-13 10:39:41  來源: .NET編程 

  <%@ 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 = RequestForm("Submit")
%>

<HTML>
<HEAD>
<META NAME="GENERATOR" Content="Microsoft Visual Studio ">
</HEAD>
<BODY>

<%
If strSubmit = "" Then
%>

注意的是
由於這是演示其中有關NT的帳號和密碼都是使用了不加密的手段在asp中傳遞的
真正的運用中應該對該登錄過程進行安全處理
<FORM action="ASPPrintasp" method=POST id=form name=form>
<TABLE WIDTH=% ALIGN=center BORDER= CELLSPACING= CELLPADDING=>
<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= cols= 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 = RequestForm("printerpath")
strUsername = RequestForm("username")
strPassword = RequestForm("password")
strMessage = RequestForm("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("ScriptingFileSystemObject")
Set objWSHNet = CreateObject("WScriptNetwork")
使用WSH連接網絡打印機
objWSHNetAddPrinterConnection "LPT" strPrinterPath False strUsername strPassword
使用文件系統對象將打印設備作為一個文件使用
Set objPrinter = objFSCreateTextFile("LPT:" True)
給打印設備送出文本
objPrinterWrite(strMessage)
關閉打印設備對象並進行錯誤陷阱處理
On Error Resume Next
objPrinterClose
如果發生錯誤關閉打印連接並輸出錯誤信息
If Err Then
ResponseWrite ("Error # " & CStr(ErrNumber) & " " & ErrDescription)
ErrClear 
Else
操作成功輸出確認信息
ResponseWrite("<CENTER>")
ResponseWrite("<TABLE WIDTH=% ALIGN=center BORDER= CELLSPACING= CELLPADDING=>")
ResponseWrite("<TR><TD ALIGN=RIGHT><B>打印消息送出:</B></TD>")
ResponseWrite("<TD ALIGN=LEFT>" & strMessage & "</TD></TR>")
ResponseWrite("<TR><TD ALIGN=RIGHT><B>網絡打印機路徑:</B></TD>")
ResponseWrite("<TD ALIGN=LEFT>" & strPrinterPath & "</TD></TR>")
ResponseWrite("<TR><TD ALIGN=RIGHT><B>登錄帳號:</B></TD>")
ResponseWrite("<TD ALIGN=LEFT>" & strUsername & "</TD></TR>")
ResponseWrite("</TABLE>")
ResponseWrite("</CENTER>")
End If
取消打印連接
objWSHNetRemovePrinterConnection "LPT:"
Set objWSHNet = Nothing
Set objFS = Nothing
Set objPrinter = Nothing
End If
%>
</BODY>
</HTML> 


From:http://tw.wingwit.com/Article/program/net/201311/14243.html
    推薦文章
    Copyright © 2005-2013 電腦知識網 Computer Knowledge   All rights reserved.