Skip to main content
InSource Solutions

TN AppSvr299 A Simple FTP Upload Script for Wonderware Application Server

Count5.JPGInSource_Logo_Transparent (1).png

 

Description

 

This article from InSource shows how to script a FTP Upload using Wonderware Application Server

  • Author: Bruce Telford
  • Published: 05/31/2018
  • Applies to: Wonderware Application Server

Details

Objective

 

To upload a file from a local location to an FTP site. For example, some customers don’t allow direct access to their ERP systems, but would rather manage the integration themselves for a variety of reasons (support and political purposes, etc.). This script, in Application Server, will transfer a file to a remote FTP site.

 

Prerequisites

To function, the following minimum information available, and if possible tested /validated with a FTP Client Software package something like Filezilla. Replace the following content with valid information “<all content between and including braces>

 

FTP Server/IP: "ftp.<domain>";

User ID: "<userid>";

Password: "<password>";

Target Location: "/inbound/";

 

Attributes

CmdSendFTP (Boolean)

FeedbackMsg (string)

FilePath (string)

FileName (string)

… all variable below can be done this way if needed

 

Script Code (Async):

 

Me. CmdSendFTP = false;             '' Resets Send FTP trigger

Me.FeedbackMsg = "";                  '' Initialize message string

 

'Variables for all FTP information

Dim sFTPServer as string;

Dim sFTPUserID as string;

Dim sFTPPassword as string;

Dim sFTPLocation as string;

 

sFTPServer = "ftp.<domain>";    '' or IP Address

sFTPUserID = "<userid>";

sFTPPassword = "<password>";

sFTPLocation = "/inbound/";       '' include "/" unless added in URI string below

 

'Variables for File information

Dim sFullFileName as string;

Dim sFilePath as string;

Dim sFileName as string;

 

sFilePath = Me.FilePath;

sFileName = Me.FileName;

sFullFileName = sFilePath + sFileName;

 

'If file exists proceed

If System.IO.File.Exists(sFullFileName) Then

 

                'Build URI for FTP Server and Upload File

                Dim uFtpTarget = new System.Uri("ftp://" + sFTPServer + sFTPLocation + sFileName);

 

                'LogMessage("DEBUG: *** " + uFtpTarget + " ***");

 

                try

                                'Create Request to Upload File

                                Dim objFtpRequest as System.Net.FtpWebRequest;

                                objFtpRequest = System.Net.WebRequest.Create(uFtpTarget);

                                objFtpRequest.Method = System.Net.WebRequestMethods.Ftp.UploadFile;

 

                                'Specify Username & Password

                                objFtpRequest.Credentials = new System.Net.NetworkCredential (sFTPUserID, sFTPPassword);

 

                                'Locate File and Store it in a Byte Array

                                Dim btFile[10240] As System.Byte;

                                btFile = System.IO.File.ReadAllBytes(sFullFileName);

 

                                'Get the File

                                Dim objStrFile As System.IO.Stream;

                                objStrFile = objFtpRequest.GetRequestStream();

 

                                'Upload Each Byte

                                objStrFile.Write(btFile, 0, btFile.Length);

 

                                'Close the file

                                objStrFile.Close();

 

                                'Free up the Memory

                                objStrFile.Dispose();

 

                catch

                                Me.FeedbackMsg = "FTP Try...Catch Error: " + error.Message.ToString();

                                LogError("ERROR: *** " + Me.FeedbackMsg + " ***");

                                LogError(error.InnerException.Message.ToString());

                endtry;

 

Else

                Me.FeedbackMsg = "INFO: File does not exist at location  -:- File: " + sFullFileName ;

                LogError("ERROR: *** " + Me.FeedbackMsg + " ***");

Endif;