Page History
...
| Code Block | ||||
|---|---|---|---|---|
| ||||
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 FTPREMOTE_FILE_URLNAME = ""; //Your FTP URLRemote full file path (must start with '/'). For example: ftp:/myfolder/mysubfolder/Hostname.com my_server_file.txt
const string USERNAME = ""; //Your Username
const string PASSWORD = ""; //Your Username 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_URL_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 {
string filePath = ""; //Enter the path of the file to upload to FTP
FtpWebRequest request = (FtpWebRequest) WebRequest.Create(string.Format("ftp://{0}\\:{1}{2}", FTP_URL, Path.GetFileName(filePath)))HOST, PORT, REMOTE_FILE_NAME));
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(USERNAME, PASSWORD);
byte[] buffer;
using (FileStream sourceStream = File.OpenRead(filePathLOCAL_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:
- The FTP URLhost, port, and path to the remote file
- The FTP server username and password
- The local file path if using the PostDatatoFTP()
Overview
Content Tools

