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 = "ftp.whatever.com"
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 [in] – 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
Else
FTPExecute = True
End If
End Function
'========================
Sub RunScript( strScriptName )
'========================
'=== Parameters: strScriptName [in] – 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") & "cmslogs" )
'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())
'response.End()
Set objFile = objFSO.OpenTextFile( strErrorLog, 2, True )
objFile.Write( ReturnCode.ReadAll() )
objFile.Close()
if not objFSO.FileExists( strLog ) then objFSO.CreateTextFile( strLog )
Set objFile = objFSO.OpenTextFile( strLog, 2, True )
objFile.Write( Output.ReadAll() )
objFile.Close()
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") & "cmslogs" )
'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"
objFile.Close
'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" )
else
Response.Write("<pre>")
end if
end if
%>
Sign up for our daily email newsletter:
You must log in to post a comment.