home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 53 / IOPROG_53.ISO / soft / c++ / xceedftp.exe / Samples / VBScript / FTPClient / FtpClient.asp next >
Encoding:
Text File  |  2000-10-05  |  9.5 KB  |  232 lines

  1. <%@ LANGUAGE="VBSCRIPT" %>
  2.  
  3. <%
  4. '--------------------------------------------------------------
  5. ' Xceed FTP Library - FTP Client sample application
  6. ' Copyright (c) 2000 Xceed Software Inc.
  7. '
  8. ' [FtpClient.asp]
  9. '
  10. ' This simple ASP sample demonstrates how to retrieve a listing of the items
  11. ' in a specifed remote folder by using the GetFolderContents method.
  12. '
  13. ' Please see documentation for details on how it works.
  14. '
  15. ' This file is part of the Xceed FTP Library sample applications
  16. ' The source code in this file is only intended as a supplement
  17. ' to Xceed FTP Library's documentation, and is provided "as is",
  18. ' without warranty of any kind, either expressed or implied.
  19. '--------------------------------------------------------------
  20.  
  21. Dim xFtp
  22. Dim sServerAddress
  23. Dim sPort
  24. Dim sUsername
  25. Dim sPassword
  26. Dim sFolder
  27.  
  28. ' Create a new instance of the XceedFtp object
  29. Set xFtp = CreateObject( "XceedSoftware.XceedFtp" )    ' reate our Xceed Ftp object
  30.  
  31. ' If you are using the free trial version of Xceed FTP Library, you'll
  32. ' need to run the Xceed FTP trial version's setup program on
  33. ' the system where your IIS server is located, otherwise the library
  34. ' will complain it is not licensed.
  35.  
  36. ' If you are a registered user, uncomment the following line
  37. ' and replace the text in quotes with your license key.
  38.         
  39. ' Call xFtp.License("your license key goes here")
  40.  
  41. sServerAddress = trim( Request( "ServerAddress" ) )
  42. sPort          = trim( Request( "Port" ) )            
  43. sUsername      = trim( Request( "Username" ) )        
  44. sPassword      = trim( Request( "Password" ) )        
  45. sFolder        = trim( Request( "Folder" ) )    ' The sFolder variable is only set when this ASP page
  46.                         ' calls itself when a folder is selected.
  47.  
  48. xFtp.ServerAddress = sServerAddress        ' Set the ServerAddresss property
  49. xFtp.ServerPort    = sPort            ' Set the ServerPort property (default = 21)
  50. xFtp.Username      = sUsername            ' Set the Username property (default = anonymous)    
  51. xFtp.Password      = sPassword            ' Set the Password property (default = guest)
  52.  
  53. On Error Resume Next
  54.  
  55. Call xFtp.Connect()    'Connect to the FTP server
  56.  
  57. If Err.Number = 0 Then
  58.    Response.Write( "<B>Connected to " & sServerAddress & "<BR><BR></B>" )    
  59.    
  60.    ' List the ObtainFolderListing procedure.
  61.    Call ObtainFolderListing( sFolder )    
  62.    
  63.    If Err.Number <> 0 Then
  64.       Response.Write( Err.Description & " #" & Err.Number & "<BR><BR>" )    
  65.    End IF
  66. Else
  67.    Response.Write( Err.Description & " #" & Err.Number & "<BR><BR>" )    
  68. End If
  69.  
  70. Set xFtp = Nothing            ' Release our instance of the XceedFtp object    
  71. Set xFolderItems = Nothing
  72.  
  73. ' --------------------------------------------------------------
  74. ' Once we are connected, we will retrieve the folder contents
  75. ' of the selected folder. The current working folder is not
  76. ' relevant if we simply call the GetFolderContents method
  77. ' and pass it the complete path and name of the folder we want
  78. ' to list the contents of.
  79. ' --------------------------------------------------------------
  80. Public Sub ObtainFolderListing( sName )
  81.  
  82. Dim xFolderItems    
  83. Dim xItem
  84. Dim sNewFolder
  85.  
  86. On Error Resume Next
  87.  
  88. ' Get a listing of the folder contents and place it in our XceedFtpFolderItems collection
  89. Set xFolderItems = xFtp.GetFolderContents( sName, 0 )
  90. Call RetrieveParentFolder( sFolder )
  91.  
  92. If Err.Number = 0 Then
  93.    Response.Write( "<TABLE WIDTH=""600"" BORDER=""2"" CELLSPACING=""0"" CELLPADDING=""0"">" & _
  94.                    "<TR  BGCOLOR=""BLUE"">" & _
  95.                       "<TD ALIGN=""CENTER""><B><FONT COLOR=""WHITE"">Date</FONT></B></TD>" & _
  96.                       "<TD ALIGN=""CENTER""><B><FONT COLOR=""WHITE"">Size (in bytes)</FONT></B></TD>" & _
  97.               "<TD ALIGN=""CENTER""><B><FONT COLOR=""WHITE"">Type</FONT></B></TD>" & _    
  98.                       "<TD ALIGN=""CENTER""><B><FONT COLOR=""WHITE"">Name</FONT></B></TD>" & _
  99.                    "</TR>" )      
  100.  
  101.       ' Loop through each item in the XceedFtpFolderItems collection and retrieve the
  102.       ' information on each item.    
  103.       For Each xItem In xFolderItems
  104.  
  105.       Select Case xItem.ItemType
  106.       Case 0    'Is a file 
  107.          Response.Write( "<TR>" & _
  108.                             "<TD WIDTH=""200"">" & xItem.Date & "</TD>" & _
  109.                             "<TD WIDTH=""100"">" & xItem.FileSize & "</TD>" & _ 
  110.                             "<TD WIDTH=""100"">File</TD>" & _
  111.                             "<TD WIDTH=""200""><A HREF=""Ftp://" & sUsername & ":" & sPassword & "@" & sServerAddress & ":" & sPort & "/" & sFolder & "/" & xItem.ItemName & """>" & xItem.ItemName & "</A></TD>" & _
  112.                          "</TR>" )
  113.  
  114.       Case 1    'Is a folder
  115.          sNewFolder = sFolder    
  116.             
  117.      If ( Len( sNewFolder ) > 0 ) Then
  118.             sNewFolder = sNewFolder & "/"
  119.          End If
  120.  
  121.          sNewFolder = sNewFolder & xItem.ItemName 'Set the new path and folder
  122.  
  123.          ' The link that will be written actualy recalls the XceedFtp.asp page and
  124.          ' gets the folder contents of the new folder specified.    
  125.      Response.Write( "<TR>" & _
  126.                             "<TD WIDTH=""200"">" & xItem.Date & "</TD>" & _
  127.                             "<TD WIDTH=""100"">" & xItem.FileSize & "</TD>" & _ 
  128.                             "<TD WIDTH=""100"">Directory</TD>" & _
  129.                             "<TD WIDTH=""200""><A HREF=""XceedFtp.asp?ServerAddress=" & sServerAddress & "&Port=" & sPort & "&Username=" & sUsername & "&Password=" & sPassword & "&Folder=" & sNewFolder & """ > " & xItem.ItemName & "</A></TD>" & _
  130.                          "</TR>" )
  131.  
  132.       Case 2    'Is a link
  133.          If LinkType( xItem.ItemName ) = "folder" then
  134.     
  135.             sNewFolder = sFolder
  136.  
  137.             If ( Len( sNewFolder ) > 0 ) Then
  138.                sNewFolder = sNewFolder & "/"
  139.             End If     
  140.   
  141.             sNewFolder = sNewFolder & xItem.ItemName ' Set the new path and folder
  142.  
  143.             Response.Write( "<TR>" & _
  144.                                "<TD WIDTH=""200"">" & xItem.Date & "</TD>" & _
  145.                                "<TD WIDTH=""100"">" & xItem.FileSize & "</TD>" & _ 
  146.                                "<TD WIDTH=""100"">Directory Link</TD>" & _
  147.                                "<TD WIDTH=""200""><A HREF=""XceedFtp.asp?ServerAddress=" & sServerAddress & "&Port=" & sPort & "&Username=" & sUsername & "&Password=" & sPassword & "&Folder=" & sNewFolder & """ > " & xItem.ItemName & "</A></TD>" & _
  148.                             "</TR>" )    
  149.          Else
  150.             If LinkType( xItem.ItemName ) = "file" Then
  151.                Response.Write( "<TR>" & _
  152.                                   "<TD WIDTH=""200"">" & xItem.Date & "</TD>" & _
  153.                                   "<TD WIDTH=""100"">" & xItem.FileSize & "</TD>" & _ 
  154.                                   "<TD WIDTH=""100"">File Link</TD>" & _
  155.                                   "<TD WIDTH=""200""><A HREF=""Ftp://" & sUsername & ":" & sPassword & "@" & sServerAddress & ":" & sPort & "/" & sFolder & "/" & xItem.ItemName & """>" & xItem.ItemName & "</A></TD>" & _
  156.                                "</TR>" )
  157.            End If
  158.     End If
  159.  
  160.       End Select    
  161.       Next
  162.    Response.Write( "</TABLE>" )
  163. Else
  164.    Response.Write( "Unable to list the files in " & sFilename & "<BR>" )
  165.    Response.Write( Err.Description & " #" & Err.Number & "<BR><BR>" )
  166. End If
  167.  
  168. End Sub
  169.  
  170. ' --------------------------------------------------------------
  171. ' The RetrieveParentFolder procedure is used to get the parent
  172. ' folder of the current remote folder. Since we are initially
  173. ' in the root folder at all times, we must change the current 
  174. ' folder to the same folder being used by the GetFolderContents 
  175. ' method before being able to retreive it's parent folder.
  176. ' --------------------------------------------------------------
  177. Public Sub RetrieveParentFolder( sName )
  178.  
  179. Dim sParentFolder 
  180. Dim nPos
  181.  
  182. If Len( sName ) > 0 Then
  183.    Call xFtp.ChangeCurrentFolder( sName )    ' Change the current remote folder
  184. End If
  185.  
  186. sName = xFtp.CurrentFolder            ' Get the current folder
  187. nPos = InStrRev( sName, "\" )
  188. sParentFolder = Mid( sName, 1, nPos )        ' Parse our folder to retrieve it's parent
  189.  
  190. Response.Write( "<TABLE WIDTH=""600"" BORDER=""0"" CELLSPACING=""0"" CELLPADDING=""0"">" & _
  191.                 "<TR>" & _
  192.                    "<TD WIDTH=""500"" ALIGN=""LEFT""><B>You are currently located in " & sFolder & "</B></TD>" & _
  193.                    "<TD WIDTH=""100"" ALIGN=""RIGHT""><A HREF=""XceedFtp.asp?ServerAddress=" & sServerAddress & "&Port=" & sPort & "&Username=" & sUsername & "&Password=" & sPassword & "&Folder=" & sParentFolder & """ >Parent Folder</A></TD>" & _
  194.                 "</TR>" & _
  195.                 "</TABLE>" )              
  196.  
  197. End Sub
  198.  
  199. ' --------------------------------------------------------------
  200. ' Some FTP servers use links instead of files or folders. However
  201. ' these links link to a file or folder than can be access in the 
  202. ' same way as a regular file or folder. Since we do not know to 
  203. ' what the link links, we will do a "test" with the 
  204. ' ChangeCurrentFolder method of the Xceed Ftp Library. If the
  205. ' link links to a file, an error will be returned and we will
  206. ' return "file". If there is no error and we were able to 
  207. ' successfully change folders, then we will return "folder".
  208. ' --------------------------------------------------------------
  209. Public Function LinkType( sName )
  210.  
  211. On Error Resume Next
  212.  
  213. Call xFtp.ChangeCurrentFolder( sName )
  214.  
  215. If Err.Number = 0 then
  216.    LinkType = "folder"
  217. Else
  218.    LinkType = "file"
  219.    Err = 0 
  220. End If
  221.  
  222. End Function
  223.  
  224. ' --------------------------------------------------------------
  225.  
  226.  
  227. %>
  228.  
  229.  
  230.  
  231.  
  232.