Link to home
Start Free TrialLog in
Avatar of Dovberman
DovbermanFlag for United States of America

asked on

how to copy a file from a local machine folder to an FTP folder

I need to copy a text file from a local folder to an ftp folder.

I believe that the steps are:

Read the local file contents into a filestream.

Initiate an FTPWebRequest.

Use a  FTPWebRequest method to copy the filestream to the ftp folder.

Here are my source statements:

   strSourcePath = "C:\\Develop\\Apps\\StockPro\\Data\\Changes\\";
   string strSourceFileName = "Changes.txt";
   string strSourceFile = strSourcePath + strSourceFileName;

// Create a filestream object

     FileStream fs = new FileStream(strSourceFile, FileMode.Open, FileAccess.Read);
     StreamReader sr = new StreamReader(fs);

// Copy the contents of the file to the request stream.
            StreamReader sourceStream = new StreamReader("Changes.txt");
            byte [] fileContents = Encoding.UTF8.GetBytes(sourceStream.ReadToEnd());

// only one column is copied to the request stream.

//Use a  FTPWebRequest method to copy the filestream to the ftp folder.
Avatar of Jerry Miller
Jerry Miller
Flag of United States of America image

Here is a function that I use to upload files to a FTP server:

Public Shared Function UploadToFTP(ByVal fstream As Stream, ByVal fileName As String, ByVal folder As String) As Boolean
        Dim reqStream As Stream
        Dim ftpWebResponse As FtpWebResponse
        Dim nc As New System.Net.NetworkCredential(ID, Password)
        Dim cc As New System.Net.CredentialCache()
        Dim strFTPFilePath As String
        Dim n_month_name As String = DateTime.Now.ToString("MMMM")
        Dim n_month_num As String = DatePart("m", DateTime.Now).ToString()
        Dim n_year As String = DatePart("yyyy", DateTime.Now).ToString()
        Dim monthFolder As String
        Dim builder As New StringBuilder

        If Len(n_month_num) = 1 Then
            builder.Append("0")
        End If
        builder.Append(n_month_num)
        builder.Append("_")
        builder.Append(n_month_name)
        monthFolder = builder.ToString()

        Try
            folder = folder & "/" & n_year & "/" & monthFolder

            'Create a FTP Request Object and Specfiy a Complete Path
            strFTPFilePath = "ftp://" & ServerName & "/" & folder & "/" & fileName

            Dim reqObj As FtpWebRequest = DirectCast(FtpWebRequest.Create(New Uri(strFTPFilePath)), FtpWebRequest)

            reqObj.Method = WebRequestMethods.Ftp.UploadFile
            'reqObj.EnableSsl = True

            'To access Resourse Protected,give UserName and PWD
            reqObj.Credentials = nc
            reqObj.UseBinary = True

            Dim buffer As Byte() = New Byte(fstream.Length - 1) {}
            fstream.Read(buffer, 0, buffer.Length)
            'Removed Close because the with the stream closed, it would not add data to the db
            'fstream.Close()

            'Upload file
            reqStream = reqObj.GetRequestStream()
            reqStream.Write(buffer, 0, buffer.Length)
            reqStream.Close()

            ftpWebResponse = reqObj.GetResponse()
            ftpWebResponse.Close()
            Return True

        Catch Ex As Exception
            commonFunctions.insertLogEvents(LOG_EVENT_ERROR, Ex.Message)
            Return False
        End Try

    End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Jerry Miller
Jerry Miller
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Dovberman

ASKER

I understand the principle, but have a few clarification questions:

// initialize parameters

string strSourcePath = "C:\\Develop\\Apps\\StockPro\\Data\\Changes\\";
string strSourceFile = strSourcePath + "Changes.txt";

string strSourceFileName = "Changes.txt";

        //Structure of the Changes//Changes.txt file
    //Date                  OldExchange        OldSymbol      NewExchange      NewSymbol
    //20131126      OTCBB          BRVM                       OTCBB                        ZENO
    //20131126      OTCBB          CCRY                      OTCBB                        ARTR

//Create filestream
 
FileStream fs = new FileStream(strSourceFile, FileMode.Open, FileAccess.Read);
StreamReader sr = new StreamReader(fs);

// ?? How do I create the filestream from the Changes.txt file?

// Copy the file to the ftp folder

Boolean blnResponse = CopyFTPFile(fs,strSourceFileName,strSourcePath);
I got it to work by making sure that the parameters were correctly structured.

Thanks,
I got it to work by making sure that the parameters were correctly structured.

Thanks,
Happy that it worked for you!