用ASP進行網絡打印功能

字號:

<%@ Language=VBScript %>
    <%
    Option Explicit
    Dim strSubmit 'Form中用來保存提交按鈕的值
    Dim strPrinterPath 'Form中保存網絡打印機路徑的值
    Dim strUsername 'Form中用戶名的值
    Dim strPassword 'Form中密碼的值
    Dim strMessage 'Form打印內容的值
    Dim objFS 'VBScript中的文件系統(tǒng)對象
    Dim objWSHNet 'WSH中的網絡對象
    Dim objPrinter '打印對象 strSubmit = Request.Form("Submit")
    %>
    
    
    
    
    
    <% If strSubmit = "" Then %> 注意的是: 由于這是演示,其中有關NT的帳號和密碼都是使用了不加密的手段在ASP中傳遞的 真正的運用中應該對該登錄過程進行安全處理。
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
網絡打印機路徑:
登錄帳號:
登錄口令:
請輸入你想打印的文字:

    
當以上信息被提交后,就可以按照下面的代碼進行打印了。
    <% 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
    ' 使用文件系統(tǒng)對象將打印設備作為一個文件使用
    Set objPrinter = objFS.CreateTextFile("LPT1:", True)
    ' 給打印設備送出文本
    objPrinter.Write(strMessage)
    '關閉打印設備對象并進行錯誤陷阱處理
    On Error Resume Next objPrinter.Close
    ' 如果發(fā)生錯誤,關閉打印連接,并輸出錯誤信息
    If Err Then Response.Write ("Error # " & CStr(Err.Number) & " " & Err.Description) Err.Clear Else
    ' 操作成功,輸出確認信息
    Response.Write("
")
    Response.Write("")
    Response.Write("")
    Response.Write("")
    Response.Write("")
    Response.Write("")
    Response.Write("")
    Response.Write("")
    Response.Write("
打印消息送出:" & strMessage & "
網絡打印機路徑:" & strPrinterPath & "
登錄帳號:" & strUsername & "
")
    Response.Write("
")
    End If
    ' 取消打印連接
    objWSHNet.RemovePrinterConnection "LPT1:"
    Set objWSHNet = Nothing
    Set objFS = Nothing
    Set objPrinter = Nothing
    End If
    %>