[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

ftpwebrequest Download 3 CSV files C#

Posted on 2016-07-28
3
Medium Priority
?
111 Views
Last Modified: 2016-08-12
I have the following code that I'm attempting to use to download 3 CSV files from a remote host and store them in the following location /site/wwwroot/ftp  on my Azure website. I am not a C# programmer so the first thing that stands out is that I notice the code does not include a line for the destination location of the downloaded files.

More importantly will this code grab all the files from the remote host or just a single file?


using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
        public static void Main()
        {
            // Get the object used to communicate with the server.
            FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://RemoteSite.com");
            request.Method = WebRequestMethods.Ftp.DownloadFile;

            // This example assumes the FTP site uses credentials.
            request.Credentials = new NetworkCredential("Username", "Password");

            FtpWebResponse response = (FtpWebResponse)request.GetResponse();

            Stream responseStream = response.GetResponseStream();
            StreamReader reader = new StreamReader(responseStream);
            Console.WriteLine(reader.ReadToEnd());

            Console.WriteLine("Download Complete, status {0}", response.StatusDescription);

            reader.Close();
            response.Close();
        }
    }
}

Open in new window

0
Comment
Question by:mburk1968
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 44

Accepted Solution

by:
zephyr_hex (Megan) earned 2000 total points
ID: 41733912
Nope.  That script doesn't attempt to get any files.  Moreover, you should know that FtpWebRequest is not meant to get multiple files.  So you'll need to use a loop which iterates over a list of the files, and downloads them one by one.

Here's the script I use in SSIS.  It based on the script found here, but I made some corrections and improvements to it.


using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Text;
using System.Collections.Generic;

namespace ST_7b4d1a22bd624bd6865788d8ccbe5c45.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {


        public void Main()
        {
	    //these are network paths to which I want to download or copy downloaded files
            string sapInPath = Dts.Variables["User::sapInPath"].Value.ToString();
            string sapHistory = Dts.Variables["User::sapHistory"].Value.ToString();
            string sapUpload = Dts.Variables["User::sapUpload"].Value.ToString();
            string sapLog = Dts.Variables["User::sapLog"].Value.ToString();
            string sapReadable = Dts.Variables["User::sapReadable"].Value.ToString();

            ftp ftpConn = new ftp("ftp://ftp.ftpHost.org", username, password);
	    //Get a list of files in the directory
            string[] pos = ftpConn.directoryListSimple("Get/");

            if (pos.Length > 0)
            {
                List<string> xmlFiles = new List<string>();
                //loop over the list of files in the list
                foreach (string po in pos)
                {
		   //check file extension and do different things based on file type
                    if (Path.GetExtension(po) == ".xml")
                    {
                        ftpConn.download("Get/" + po, sapInPath + sapHistory + po);
                        xmlFiles.Add(po);
                        ftpConn.delete("Get/" + po);
                        File.Copy(sapInPath + sapHistory + po, sapInPath + sapUpload + po);
                    }
                    else if (Path.GetExtension(po) == ".html")
                    {
                        ftpConn.download("Get/" + po, sapReadable + po);
                        ftpConn.delete("Get/" + po);
                    }
                }

		//create a log file
                string logFile = "xml_b2b_po_" + DateTime.Now.ToString("yyyy-MM-dd_hh-mm") + ".xml";
                File.WriteAllLines(sapInPath + sapLog + logFile, xmlFiles.ToArray(), Encoding.UTF8);
            }
            ftpConn = null;

            Dts.TaskResult = (int)ScriptResults.Success;
        }

        class ftp
        {
            private string host = null;
            private string user = null;
            private string pass = null;
            private FtpWebRequest ftpRequest = null;
            private FtpWebResponse ftpResponse = null;
            private Stream ftpStream = null;
            private int bufferSize = 2048;

            /* Construct Object */
            public ftp(string hostIP, string userName, string password) { host = hostIP; user = userName; pass = password; }

            /* Download File */
            public void download(string remoteFile, string localFile)
            {
                try
                {
                    /* Create an FTP Request */
                    ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + remoteFile);
                    /* Log in to the FTP Server with the User Name and Password Provided */
                    ftpRequest.Credentials = new NetworkCredential(user, pass);
                    /* When in doubt, use these options */
                    ftpRequest.UseBinary = true;
                    ftpRequest.UsePassive = true;
                    ftpRequest.KeepAlive = true;
                    /* Specify the Type of FTP Request */
                    ftpRequest.Method = WebRequestMethods.Ftp.DownloadFile;
                    /* Establish Return Communication with the FTP Server */
                    ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                    /* Get the FTP Server's Response Stream */
                    ftpStream = ftpResponse.GetResponseStream();
                    /* Open a File Stream to Write the Downloaded File */
                    FileStream localFileStream = new FileStream(localFile, FileMode.Create);
                    /* Buffer for the Downloaded Data */
                    byte[] byteBuffer = new byte[bufferSize];
                    int bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
                    /* Download the File by Writing the Buffered Data Until the Transfer is Complete */
                    try
                    {
                        while (bytesRead > 0)
                        {
                            localFileStream.Write(byteBuffer, 0, bytesRead);
                            bytesRead = ftpStream.Read(byteBuffer, 0, bufferSize);
                        }
                    }
                    catch (Exception ex) 
                    { 
                        throw new Exception("Error reading download stream from remote FTP server: ", ex); 
                    }
                    /* Resource Cleanup */
                    localFileStream.Close();
                    ftpStream.Close();
                    ftpResponse.Close();
                    ftpRequest = null;
                }
                catch (Exception ex) 
                {
                    throw new Exception("Error with download request to remote FTP server: ", ex);
                }
                return;
            }

            public string[] directoryListSimple(string directory)
            {
                try
                {
                    /* Create an FTP Request */
                    ftpRequest = (FtpWebRequest)FtpWebRequest.Create(host + "/" + directory);
                    /* Log in to the FTP Server with the User Name and Password Provided */
                    ftpRequest.Credentials = new NetworkCredential(user, pass);
                    /* When in doubt, use these options */
                    ftpRequest.UseBinary = true;
                    ftpRequest.UsePassive = false;
                    ftpRequest.KeepAlive = true;
                    /* Specify the Type of FTP Request */
                    ftpRequest.Method = WebRequestMethods.Ftp.ListDirectory;
                    /* Establish Return Communication with the FTP Server */
                    ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                    /* Establish Return Communication with the FTP Server */
                    ftpStream = ftpResponse.GetResponseStream();
                    /* Get the FTP Server's Response Stream */
                    StreamReader ftpReader = new StreamReader(ftpStream);
                    /* Store the Raw Response */
                    string directoryRaw = null;
                    /* Read Each Line of the Response and Append a Pipe to Each Line for Easy Parsing */
                    try 
                    { 
                        while (ftpReader.Peek() != -1) 
                        { 
                            directoryRaw += ftpReader.ReadLine() + "|"; 
                        } 
                    }
                    catch (Exception ex) 
                    {
                        throw new Exception("Error reading directory listing on remote FTP server: ", ex); 
                    }
                    /* Resource Cleanup */
                    ftpReader.Close();
                    ftpStream.Close();
                    ftpResponse.Close();
                    ftpRequest = null;
                    /* Return the Directory Listing as a string Array by Parsing 'directoryRaw' with the Delimiter you Append (I use | in This Example) */
                    try 
                    { 
                        string[] directoryList = directoryRaw.Split("|".ToCharArray()); return directoryList; 
                    }
                    catch (Exception ex) 
                    {
                        throw new Exception("Error parsing directory listing from remote FTP server: ", ex);  
                    }
                }
                catch (Exception ex) 
                {
                    throw new Exception("Error getting directory listing from remote FTP server: ", ex); 
                }
            }

            public void delete(string deleteFile)
            {
                try
                {
                    /* Create an FTP Request */
                    ftpRequest = (FtpWebRequest)WebRequest.Create(host + "/" + deleteFile);
                    /* Log in to the FTP Server with the User Name and Password Provided */
                    ftpRequest.Credentials = new NetworkCredential(user, pass);
                    /* When in doubt, use these options */
                    ftpRequest.UseBinary = true;
                    ftpRequest.UsePassive = true;
                    ftpRequest.KeepAlive = true;
                    /* Specify the Type of FTP Request */
                    ftpRequest.Method = WebRequestMethods.Ftp.DeleteFile;
                    /* Establish Return Communication with the FTP Server */
                    ftpResponse = (FtpWebResponse)ftpRequest.GetResponse();
                    /* Resource Cleanup */
                    ftpResponse.Close();
                    ftpRequest = null;
                }
                catch (Exception ex) 
                {
                    throw new Exception("Error deleting file on remote FTP server: ", ex); 
                }
                return;
            }
        }
    }
}

Open in new window

0
 
LVL 44

Expert Comment

by:zephyr_hex (Megan)
ID: 41734064
I should mention:  That script deletes the file from the ftp server after downloading it.  If you don't want to delete the files, you can remove lines 41 and 47 (and the delete method in the ftp class)
0
 

Author Closing Comment

by:mburk1968
ID: 41753882
Thank You
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…

649 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question