The new FTP component comes with code to upload and download files from an FTP server. The sample code is below:

using System;
using StresStimulus.Extensibility;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.IO;

class MyFTPComponent : ActionComponent //Do not rename Class
{
	public MyFTPComponent() {
		//Do not edit this section.
	}

#region Start/Stop Test

	/// <summary>
	/// Fired when test started.
	/// </summary>
	public override void OnTestStart() {

	}

	/// <summary>
	/// Fired when test ends.
	/// </summary>
	public override void OnTestEnd() {

	}

#endregion

    const string FTP_HOST = ""; //server name or IP
	const int PORT = 21; //port number. 21 is default
	const string REMOTE_FILE_NAME = ""; //Remote full file path (must start with '/'). For example: /myfolder/mysubfolder/my_server_file.txt
	const string USERNAME = ""; //Your Username
	const string PASSWORD = ""; //Your password
	const string LOCAL_FILE_PATH = ""; //Full path of the local file to upload. For example c:\\myfolder\myfile.txt	
	const int BUFFER_SIZE = 1024; //File read/write buffer size.
	
	public void DownloadFileFromFtp(ActionArgs arg) {
		try {
			FtpWebRequest request = (FtpWebRequest) WebRequest.Create(string.Format("ftp://{0}:{1}{2}", FTP_HOST, PORT, REMOTE_FILE_NAME));
			request.Method = WebRequestMethods.Ftp.DownloadFile;
			request.Credentials = new NetworkCredential(USERNAME, PASSWORD);
			request.KeepAlive = false;
			request.UseBinary = true;
			request.UsePassive = true;
			using (FtpWebResponse response = (FtpWebResponse) request.GetResponse()) {
				using (Stream responseStream = response.GetResponseStream()) {
					byte[] buffer = new byte[BUFFER_SIZE];

					int read = responseStream.Read(buffer, 0, buffer.Length);

					while (read > 0) {
						read = responseStream.Read(buffer, 0, buffer.Length);
					}
				}
			}
		}
		catch (Exception ex) {
			arg.IsError = true;
			arg.ErrorMessage = ex.Message;
		}
	}

	public void PostDatatoFTP(ActionArgs arg) {
		try {
			FtpWebRequest request = (FtpWebRequest) WebRequest.Create(string.Format("ftp://{0}:{1}{2}", FTP_HOST, PORT, REMOTE_FILE_NAME));
			request.Method = WebRequestMethods.Ftp.UploadFile;
			request.Credentials = new NetworkCredential(USERNAME, PASSWORD);
			byte[] buffer;
			using (FileStream sourceStream = File.OpenRead(LOCAL_FILE_PATH)) {
				buffer = new byte[sourceStream.Length];
				sourceStream.Read(buffer, 0, buffer.Length);
			}
			request.ContentLength = buffer.Length;
			using (Stream requestStream = request.GetRequestStream()) {
				requestStream.Write(buffer, 0, buffer.Length);
			}
			using (FtpWebResponse response = (FtpWebResponse) request.GetResponse()) {
			}
		}
		catch (Exception ex) {
			arg.IsError = true;
			arg.ErrorMessage = ex.Message;
		}
	}
}

Add the following to the code:

  1. The FTP host, port, and path to the remote file
  2. The FTP server username and password
  3. The local file path if using the PostDatatoFTP() 


  • No labels