0
Answer

Question on FTP

moises cujardo

moises cujardo

15y
3.6k
1
hi to all,

I am currently new to C# and I am writing very basic C# FTP application.
Right now, I writing the code to download a file from an FTP Server then save
the downloaded files to the local machine. Below is the
code snippet of what I have:

    //set file path
                    filePath = pathToStore + fileNames[i];

                    //instantiate output stream object
                    //@FileMode.Create, we want to write the file on the specified filePath
                    _outputStream = new FileStream(filePath, FileMode.Create);

                    //create ftpwebrequest object
                    //file path on the server
                    string file = this._server + "testings/csv/" + fileNames[i];
                    Console.WriteLine(file);
                    _ftpReq = (FtpWebRequest)FtpWebRequest.Create(file);

                    //specify network credintial, ther username and password
                    //required to use the FTP server
                    _ftpReq.Credentials = new NetworkCredential(this._username, this._password, this._domain);

                    //specify the method we need for this action
                    _ftpReq.Method = WebRequestMethods.Ftp.DownloadFile;
                    _ftpReq.UseBinary = true;

                    //get server response
                    _ftpResponse = (FtpWebResponse)_ftpReq.GetResponse();
                   
                    //comment needed here
                    _ftpStream = _ftpResponse.GetResponseStream();

                    //comment needed here
                    long cl = _ftpResponse.ContentLength;

                    int bufferSize = 1024;                   //specify buffer size to 512MB
                    int readCount;                          //declare counter variable
                    byte[] buffer = new byte[bufferSize];   //declare the buffer

                    //comment needed here
                    readCount = _ftpStream.Read(buffer, 0, bufferSize);

                    //read stream content and store to
                    //local machine or the specified dir.
                    while (readCount > 0)
                    {
                        _outputStream.Write(buffer, 0, readCount);
                        readCount = _ftpStream.Read(buffer, 0, bufferSize);
                    }

My problem is, once the download have been completed, all files that
were downloaded are 0kb... all file sizes are 0kb...  I checked the files
on the FTP Server and all files are greater than 100Kbs.

Please help me with this. Any oppiniouns and help are greatly appreciated
Thanks


ga-ara