This week’s Tech Tuesday is from our very own Chris Dew, who will explain how to create a task in SmartConnect to get a file from an FTP site.
We get quite a few customers that want to take files from a FTP site and utilize this file as the source of their data when running a map inside of SmartConnect. Today I have created a sample Task script that will allow you to grab a file in a Pre Integration Task and then use it for the integration.
First you can simply click on the Tasks button in any existing map, and then choose a new task called run script. This is where you can now copy the script below and paste into this window.
'EXAMPLE OF HOW TO USE FtpWebRequest 'Configuration Const localFile As String = "C:UsersAdministratorDownloadsftpREADME.txt" Const remoteFile As String = "/README.txt" Const host As String = "ftp://ftp.swfwmd.state.fl.us/pub/" Const username As String = "anonymous" Const password As String = "email@email.com" 'Create a request Dim URI As String = host & remoteFile Dim ftp As System.Net.FtpWebRequest = CType(System.Net.FtpWebRequest.Create(URI), System.Net.FtpWebRequest) 'Set the credentials ftp.Credentials = New System.Net.NetworkCredential(username, password) 'Turn off KeepAlive (will close connection on completion) ftp.KeepAlive = False 'we want a binary ftp.UseBinary = True 'Define the action required (in this case, download a file) ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile 'If we were using a method that uploads data e.g. UploadFile 'we would open the ftp.GetRequestStream here an send the data 'Get the response to the Ftp request and the associated stream Using response As System.Net.FtpWebResponse = CType(ftp.GetResponse, System.Net.FtpWebResponse) Using responseStream As IO.Stream = response.GetResponseStream 'loop to read & write to file Using fs As New IO.FileStream(localFile, IO.FileMode.Create) Dim buffer(2047) As Byte Dim read As Integer = 0 Do read = responseStream.Read(buffer, 0, buffer.Length) fs.Write(buffer, 0, read) Loop Until read = 0 'see Note(1) responseStream.Close() fs.Flush() fs.Close() End Using responseStream.Close() End Using response.Close() End Using return true
The only configuration that needs to be done is at the very top where you determine four things:
localFile: The name and location you want to put the file on your network
remoteFile: The name of the remote file
host: FTP Site
username: Username
password: Password
Have fun with this one and use your imagination on other scripts you can use. If you have a suggestion for a new script please let us know.
Interested in SmartConnect? Please email us at sales@eonesolutions.com and we would be happy to assist you!