ftp/ftplib.asp
<%@ Language="VBScript"%>
<!--METADATA NAME="SmartFTP FTP Library" TYPE="TypeLib" UUID="{7A3A786C-EB8C-43b3-BC10-8D09ACF5D195}"-->
<%
'///////////////////////////////////////////////////////////////////////////////////
'//
'// SmartFTP Library ASP Demo v1.1 (4. May 2005)
'//
'// Purpose:
'// Demonstrates the use of the sfFTPLib component with ASP.
'// Connects, lists and downloads files from a remote server.
'//
'// Todo:
'// Code doesn't recognize disconnects => Automatic reconnects
'//
'// Copyright (c) 2004 - 2005 by SmartFTP.com
'//
'///////////////////////////////////////////////////////////////////////////////////
' Settings
' Temporary folder for file downloads.
' Create the folder in your web and give read/write permissions to the IWAM_ user
const cTempFolder = "/temp"
' Log folder
' Create the folder in your web and give read/write permissions to COM user
const cLogFolder = "/log"
Dim g_objFTP
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>SmartFTP Library ASP Demo</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="author" content="SmartFTP.com">
</head>
<body>
<style type="text/css">
<!--
body {
color : #000000;
background-color: #FFFFFF;
font: normal 12px Arial, Helvetica, sans-serif;
}
-->
</style>
</head>
<h3>SmartFTP Library ASP Demo</h3>
<p>
This sample will connect to remote server and allow you to list directories and download files.</p>
<p><a href="?a=connect">Connect</a> | <a href="?a=disconnect">Disconnect</a> | <a href="?a=list">List</a></p>
<%
'//////////////////////////////////////////
'// List all files in a specified folder
'//
'// Note:
'// - How to convert unicode to UTF8
'// http://www.alleged.org.uk/pdc/2003/utf8.html
'//
Private Function List(sFolder)
Dim err : err = 0
If Len(sFolder) > 0 Then
err = g_objFTP.ChangeDirectory(sFolder)
End If
If err = ftpErrorSuccess Then
err = g_objFTP.ReadDirectory()
If err = ftpErrorSuccess Then
Dim objDirectory
Set objDirectory = g_objFTP.Items
Dim strMsg : strMsg = ""
Response.Write("Directory: <a href=""?a=list&dir=" & g_objFTP.WorkingDirectory & """>" & g_objFTP.WorkingDirectory & "</a> Objects: " & objDirectory.Count & "<br>")
Response.Write("<table>")
If g_objFTP.WorkingDirectory <> "/" Then
Response.Write("<tr><td><a href=""?a=list&dir=..""><img border=""0"" src=""img/parent.png""></a></td><td><a href=""?a=list&dir=.."">Parent</a></td><td></td><td></td></tr>")
End If
Dim FTPFile
For Each FTPFile In objDirectory
Dim sLine
sLine = "<tr><td>"
If FTPFile.Type = ftpItemTypeFolder Then
sLine = sLine & "<a href=""?a=list&dir=" & FTPFile.Name & """><img border=""0"" src=""img/folder.png""></a>"
sLine = sLine & "</td><td>"
sLine = sLine & "<a href=""?a=list&dir=" & FTPFile.Name & """>" & FTPFile.Name & "</a>"
sLine = sLine & "</td><td>"
sLine = sLine & "<Dir>"
sLine = sLine & "</td>"
ElseIf FTPFile.Type = ftpItemTypeLink Then
sLine = sLine & "<a href=""?a=list&dir=" & FTPFile.Name & """><img border=""0"" src=""img/link.png""></a>"
sLine = sLine & "</td><td>"
sLine = sLine & "<a href=""?a=list&dir=" & FTPFile.Name & """>" & FTPFile.Name & "</a>"
sLine = sLine & "</td><td>"
sLine = sLine & "<Link>"
sLine = sLine & "</td>"
Else
sLine = sLine & "<a href=""?a=download&file=" & FTPFile.Name & """><img border=""0"" src=""img/file.png""></a>"
sLine = sLine & "</td><td>"
sLine = sLine & "<a href=""?a=download&file=" & FTPFile.Name & """>" & FTPFile.Name & "</a>"
sLine = sLine & "</td><td>"
sLine = sLine & "<File>"
sLine = sLine & "</td>"
End If
sLine = sLine & "<td align=""right"">" & FTPFile.SizeLo & "</td><td align=""right"">" & FTPFile.Date & "</td><td>" & Hex(FTPFile.UnixAttributes) & "<td></tr>"
Response.Write(sLine & vbCrLf)
Next
Response.Write("</table>")
Set objDirectory = Nothing
End If
End If
List = err
End Function
'/////////////////////////////////////////
'// IsLocalFolderWriteable
Private Function IsLocalFolderWriteable(sFolder)
Dim bRet : bRet = false
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")
Dim sTempFileName : sTempFileName = fso.GetTempName()
Dim sFile
sFile = sFolder & "\" & sTempFileName
Dim oFile
On Error Resume Next
Set oFile = fso.CreateTextFile(sFile, True)
If Err = 0 Then
oFile.Close
fso.DeleteFile(sFile)
bRet = true
End If
IsLocalFolderWriteable = bRet
End Function
'/////////////////////////////////////////
'// Downloads file and write content to the output stream
Private Function DownloadFile(sFile)
Dim bRet : bRet = false
' Note:
' FSO.GetSpecialFolder(2) doesn't work because IIS doesn't have read permissions to this folder.
' Get temp file
set FSO = Server.CreateObject("Scripting.FileSystemObject")
Dim sTempFile
Dim sTempFolder
Dim sTempFileName : sTempFileName = FSO.GetTempName()
' Warning: Do not use the special folder unless the IUSR_ and IWAM_ account have read/write permissions.
'Dim objFolder
'Set objFolder = FSO.GetSpecialFolder(2)
'sTempFolder = objFolder
' Use const temp folder.
' Important: Create it and give read/write permissions to the IWAM_ user
sTempFolder = Server.MapPath(cTempFolder)
If IsLocalFolderWriteable(sTempFolder) Then
sTempFile = sTempFolder & "\" & sTempFileName
'Response.Write("TempFile = " & sTempFile & vbCrLf)
Dim result
result = g_objFTP.DownloadFile(sFile, sTempFile, 0, 0)
If result = ftpErrorSuccess Then
Response.Buffer = True
Response.Clear
set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1 'binary type
objStream.LoadFromFile sTempFile
Response.ContentType = "application/octet-stream"
Response.ExpiresAbsolute = #1/1/1980#
Response.AddHeader "pragma", "no-cache"
Response.AddHeader "cache-control", "no-cache, private, must-revalidate"
Response.AddHeader "Content-Disposition", "filename=""" & sFile & """"
Response.AddHeader "Connection", "close"
Response.BinaryWrite objStream.Read
objStream.Close
Response.Flush
Response.End
FSO.DeleteFile sTempFile
bRet = true
End If
Else
Response.Write("The folder '" & sTempFolder & "' is not writeable. Set the right permissions.<br>" & vbCrLf)
End If
DownloadFile = bRet
End Function
' Entry
Dim sAction
If Len(Request.QueryString("a")) = 0 Then
sAction = "connect"
Else
sAction = Request.QueryString("a")
End If
' Disconnect
If sAction = "disconnect" Then
If IsObject(Session("object")) Then
Set g_objFTP = Session("object")
g_objFTP.Disconnect
Set g_objFTP = Nothing
Response.Write("Disconnected.")
Session.Contents.Remove("object")
Else
Response.Write("Nothing to disconnect.")
End If
End If
' List
If sAction = "list" Then
If not IsObject(Session("object")) Then
Response.Write("FTPConnection session object not found. Connect first.")
Else
Set g_objFTP = Session("object")
Dim result
result = List(Request.QueryString("dir"))
If result = ftpErrorSuccess Then
Else
Response.Write("List failed. Error = " & result)
End If
End If
End If
' Download File
If sAction = "download" Then
If IsObject(Session("object")) Then
Set g_objFTP = Session("object")
Dim sFile
sFile = Request.QueryString("file")
If Len(sFile) > 0 Then
Dim bRes
bRes = DownloadFile(sFile)
If Not bRes Then
Response.Write("DownloadFile() failed. Error = " & g_objFTP.LastError)
End If
Else
Response.Write("File not specified.")
End If
Else
Response.Write("FTPConnection session object not found. Connect first.")
End If
End If
' Connect
If sAction = "connect" Then
If Not IsObject(Session("object")) Then
If Len(Request.Form("host")) > 0 Then
Response.Write ("Initializing SmartFTP Library<BR>")
Response.Flush
Set g_objFTP = Server.CreateObject("sfFTPLib.FTPConnectionSTA")
g_objFTP.Host = Request.Form("host")
g_objFTP.Port = Request.Form("port")
g_objFTP.Protocol = Request.Form("protocol")
g_objFTP.Username = Request.Form("username")
g_objFTP.Password = Request.Form("password")
' Log File
Dim FSO
Set FSO = Server.CreateObject("Scripting.FileSystemObject")
Dim sLogFile
Dim sTempFolder
Dim sTempFileName : sTempFileName = FSO.GetTempName()
' Use const temp folder.
' Important: Create the temp folder in your web and give read/write permissions to the IWAM_ user if you want the log
sTempFolder = Server.MapPath(cLogFolder)
sLogFile = sTempFolder & "\" & sTempFileName & ".log"
'Response.Write("LogFile = " & sTempFile & vbCrLf)
g_objFTP.LogFile = sLogFile
Response.Write ("Connecting...<BR>")
Response.Flush
If g_objFTP.Connect = ftpErrorSuccess Then
Set Session("object") = g_objFTP
Response.Write("Connected.<BR>")
Response.Flush
List("")
Else
Response.Write("Connect() failed. Error = " & g_objFTP.LastError)
Set g_objFTP = Nothing
End If
Else
%>
<form method="POST" action="<%=Request.ServerVariables("SCRIPT_NAME")%>">
<input type="hidden" name="a" value="connect">
<table>
<tr>
<td>Protocol</td><td><select name="protocol">
<option value="<%=ftpProtocolNormal%>">FTP Normal</option>
<option value="<%=ftpProtocolSSLImplicit%>">FTP SSL Implicit</option>
<option value="<%=ftpProtocolSSLExplicit%>">FTP SSL Explicit</option>
</select>
</td>
</tr>
<tr><td>Host</td><td><input type="text" name="host" value="ftp.smartftp.com"></td></tr>
<tr><td>Port</td><td><input type="text" name="port" value="21"></td></tr>
<tr><td>Username</td><td><input type="text" name="username" value="anonymous"></td></tr>
<tr><td>Password</td><td><input type="text" name="password" value="test@test.com"></td></tr>
<tr><td colspan="2"><input type="submit" value="Connect"></td></tr>
</table>
</form>
<%
End If
Else
Response.Write("Already connected. Disconnect first.")
End If
End If
%>
</body>
</html>