ASP Code

FTP Files From an ASP Web Page/Application

Embed file transfer functionality into your application, without downloading and installing server-side components, or spending any money. You can find the full explanation of this code in an article I wrote.

Apparently, with windows server 2003, the IIS application pool that the website is running under needs to use the identity
of a particular user with the appropriate permissions, rather than the predefined identity.

This ASP code uses ftp.exe, the built-in command line FTP client with Windows to do the uploading.

dim strHost, UserName, Password, Mode, LocalDir, RemoteDir, Output, ReturnCode, strScript

Const FTP_CMD 	= "ftp.exe -i -s:"
strHost 		= ""
UserName  		= "user"
Password 		= "pass"
Mode 	 		= "ascii"	'Set this to "ascii" or "binary" 
LocalDir  		= Request.ServerVariables("APPL_PHYSICAL_PATH") & "files"
RemoteDir 		= "/incoming"

sub setRemoteDir( newDir )
	RemoteDir = newDir
end sub

sub setLocalDir( newDir )
	localDir = newDir
end sub

sub setMode( newMode )
	Mode = newMode
end sub

Function FTPExecute( strCMD )
'=== Builds an FTP command script, runs it, then deletes it.
'=== Parameters: strCMD &#91;in&#93; - The FTP command to run.
	Dim strFile, objFSO
	strFile = BuildFTPCmdScript( strCMD )
	RunScript( strFile )
	'Delete the temporary command script
	Set objFSO = CreateObject("Scripting.FileSystemObject")
	objFSO.DeleteFile strFile, True
	set objFSO = nothing
	'Create a regular expression.
	Dim regEx
	Set regEx = New RegExp			
	regEx.IgnoreCase = True
	'Search the output for indications of failure
	regEx.Pattern = "Not connected|Invalid command|Error"
	If ( regEx.Test( Output.ReadAll ) = True ) or ( regEx.Test( ReturnCode.ReadAll ) ) Then
		FTPExecute = False
		FTPExecute = True
	End If
End Function

Sub RunScript( strScriptName )
'=== Parameters: strScriptName &#91;in&#93; - The full path name of the script to run.
	dim objShell, wsx, strLog, strErrorLog, objFSO, objTempFldr, objFile
	Set objFSO 	 = CreateObject("Scripting.FileSystemObject")
	Set objShell = Server.CreateObject("WScript.Shell")
	set wsx 		= objShell.Exec( FTP_CMD & strScriptName & " " & strHost )
	set ReturnCode 	= wsx.StdErr
	set Output 		= wsx.stdOut
	set objTempFldr = objFSO.GetFolder( Request.ServerVariables("APPL_PHYSICAL_PATH") & "cms\logs\" )
	'set objTempFldr = objFSO.GetSpecialFolder( 2 )
	strErrorLog 	= objTempFldr.Path & "\ftp_error_log.txt"
	strLog 			= objTempFldr.Path & "\ftp_shell_log.txt"
	if not objFSO.FileExists( strErrorLog ) then objFSO.CreateTextFile( strErrorLog )
	'response.Write("<pre>" & Output.ReadAll())
	Set objFile = objFSO.OpenTextFile( strErrorLog, 2, True )
	objFile.Write( ReturnCode.ReadAll() )
	if not objFSO.FileExists( strLog ) then objFSO.CreateTextFile( strLog )
	Set objFile = objFSO.OpenTextFile( strLog, 2, True )
	objFile.Write( Output.ReadAll() )
	set objFSO  = nothing
	set objFile = nothing
End Sub

Function BuildFTPCmdScript( strCMD )
'=== Builds an FTP command script
'=== Parameters strCMD:[in] - The FTP command to write to the script.
'=== Returns the full path to the command script.

	'Get a temporary file name
	Dim objTempFldr, strCMDFileName, objFSO
	Set objFSO 		= CreateObject("Scripting.FileSystemObject")
	set objTempFldr	= objFSO.GetFolder( Request.ServerVariables("APPL_PHYSICAL_PATH") & "cms\logs\" )
	'set objTempFldr = objFSO.GetSpecialFolder( 2 )
	strCMDFileName	= objFSO.GetTempName
	'Create a file in the 'temp' folder
	Dim strCMDFilePath, objFile
	strCMDFilePath = objTempFldr & "\" & strCMDFileName & ".ftp"
	if not objFSO.FileExists( strCMDFilePath ) then objFSO.CreateTextFile( strCMDFilePath )
	Set objFile = objFSO.OpenTextFile( strCMDFilePath, 2, True )

	'Write the commands
	objFile.WriteLine UserName
	objFile.WriteLine Password
	If LocalDir <> "" Then
		objFile.WriteLine "lcd " & LocalDir
	End If
	If RemoteDir <> "" Then
		objFile.WriteLine "cd " & RemoteDir
	End If
	objFile.WriteLine Mode
	objFile.WriteLine strCMD
	objFile.WriteLine "bye"
	'Return the complete path to the file
	BuildFTPCmdScript = strCMDFilePath

End Function

dim blTest
blTest = 0

strScript = "cd downloads" & vbCrLf & _
			"mkd test" & vbCrLf & _
			"mkd test" & vbCrLf & _
			"mkd test" & vbCrLf

if blTest then
	if FTPExecute( strScript ) then
		Response.Write( "good" )
	end if
end if


  1. Hi, I hope please you remember this and can help me.

    I'm programing an asp page where I'm supposed to send a file from mi local computer to a server, I'm not sure if your applications works this way or if it transfer files from the webserver to an ftp server.
    What do this line means? mi localdir is the directory in the web server where is hosted my applicacion? or it is on mi locacl computer?
    LocalDir=Request.ServerVariables("APPL_PHYSICAL_PATH") & "files"

    If it is in my web server I think I can upload files to this directory from my page and the ftp to the ftp server (that is in my intranet), the problems is that I upload this cose to my web server and I vave no error and no message about nothing, and it didn't created any directory.

    Thanks in advance,

Leave a Reply

Your email address will not be published. Required fields are marked *

To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image