网站首页 > 书籍教程 > ASP教程 > WEB打印设置解决方案四(在ASP中实现网络打印功能)

WEB打印设置解决方案四(在ASP中实现网络打印功能)

  • 作者:互联网
  • 时间:2009-06-24 17:27:33

使用到的技术:  
ASP,WSH,VBScript  
文件AS***int.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 = Re***st.Form("Submit")  
%>  

    
    
    
    
    
 
<%  
     If strSubmit = "" Then  
%>  

  注意的是:  
      由于我是演示起见,其中有关NT的帐号和密码都是使用了不加密的手段在ASP中传递的真正的运用中应该对该登录过程进行安全处理。  
 

AS***int.asp" method=POST id=form name=form>  
       
           
                
                
           
           
                
                
           
           
                
                
           
           
               
               
           
           
               
               
           
    
网络打印机路径:
登录帐号:              value="<% = strUsername %>">
登录口令:
请输入你想打印的文字:
  
 
  

  当以上信息被提交后,就可以按照下面的代码进行打印了。  
  <%  
  Else  
     ' 从form中取得响应信息。  
     strPrinterPath = Re***st.Form("printerpath")  
     strUsername = Re***st.Form("username")  
     strPassword = Re***st.Form("password")  
     strMessage = Re***st.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连接网络打印机
     Set objWSHNet = CreateObject("WS***pt.Network")     
     ob***HNet.AddPrinterConnection "LPT1", strPrinterPath, False, strUsername, strPassword  
     '使用文件系统对象将打印设备作为一个文件使用  
     Set objFS = CreateObject("Sc***ting.FileSystemObject")
     Set objPrinter = ob***.CreateTextFile("LPT1:", True)  
     ' 给打印设备送出文本  
     ob***inter.Write(strMessage)  
     '关闭打印设备对象并进行错误陷阱处理  
     On Error Resume Next  
     ob***inter.Close 
     ' 如果发生错误,关闭打印连接,并输出错误信息  
     If Err Then  
        Re***nse.Write ("Error # " & CStr(Er***umber) & " " & Er***escription)  
        Er***lear ;     
     Else  
        ' 操作成功,输出确认信息  
        Re***nse.Write("

")  
        Re***nse.Write("")  
        Re***nse.Write("")  
        Re***nse.Write("")  
        Re***nse.Write("")  
        Re***nse.Write("")  
        Re***nse.Write("")  
        Re***nse.Write("")  
        Re***nse.Write("
打印消息送出:" & strMessage & "
网络打印机路径:" & strPrinterPath & "
登录帐号:" & strUsername & "
")  
        Re***nse.Write("
")  
     End If  
     ' 取消打印连接  
     ob***HNet.RemovePrinterConnection "LPT1:"  
     Set objWSHNet  = Nothing  
     Set objFS      = Nothing  
     Set objPrinter = Nothing  
  End If  
  %>