Solved

ftpwebrequest Download 3 CSV files C#

Posted on 2016-07-28
3
36 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
  • 2
3 Comments
 
LVL 42

Accepted Solution

by:
zephyr_hex earned 500 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 42

Expert Comment

by:zephyr_hex
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now