Click here to Skip to main content
15,906,106 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to know weather the files is fully downloaded in ftpserver from local system.
I written a code that will download the files which are dropped in certain folder path in ftp server.my problem is when a file of size above 2 MB is dropped in ftpserver folder before it gets fully downloaded in my code it starts downloading the file and finally it download half of the content I want to find that the file in ftpserver is fully downloaded and then only i want to start download to local system. here is my code please any one reply me i searched for more than four days. Thanks in advance.

C#
while (fileQueue.Count > 0)
            {
                string fileName = fileQueue.Dequeue();
                int getindex = fileName.LastIndexOf("/");
                string getsubfile = fileName.Substring(getindex, fileName.Length - getindex);                
                
                string filePath = localPath;
                string srnew = fileName.Replace("ftp://" + ftpServerIP + ftpPath, " ");
                FtpWebRequest reqFTP;
                FileStream outputStream;

                try
                {
                    //reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + ftpFolderName + fileName));
                    reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri(fileName));
                    reqFTP.Method = WebRequestMethods.Ftp.DownloadFile;
                    reqFTP.UseBinary = true;
                    reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
                    FtpWebResponse response = (FtpWebResponse)reqFTP.GetResponse();
                    Stream ftpStream = response.GetResponseStream();
                    long cl = response.ContentLength;
                    // this.Invoke(new MethodInvoker(delegate { dataGridView1.Rows[count].Cells[3].Value = cl; }));
                    int bufferSize = 2048;
                    int readCount;
                    byte[] buffer = new byte[bufferSize];
                    //Random rand = new Random();
                    // byte nextByte;  
                     int folderindex = srnew.LastIndexOf("/");
                     string folderfile = srnew.Substring(0, folderindex);                              
                    if (folderfile != string.Empty)
                    {
                        if (Directory.Exists(localPath +folderfile))
                        {

                        }
                        else
                        {
                            Directory.CreateDirectory(localPath + folderfile);
                            //writetextDownAfter(localPath + sr.Trim() + "/" + strname.Trim());
                        }
                    }
                    readCount = ftpStream.Read(buffer, 0, bufferSize);
                    int i = 1;
                    while (File.Exists(filePath + srnew.Trim()))
                    {
                        string[] over = srnew.Split('.');
                        over[0] = over[0] + i.ToString();
                        srnew = over[0] + "." + over[1];
                        i++;
                    }

                    outputStream = new FileStream(filePath + srnew, FileMode.Create);
                    //Stopwatch sw = new Stopwatch();
                    //sw.Start();
                    this.Invoke(new MethodInvoker(delegate { dataGridView1.Rows.Add("Server1", getsubfile.ToString(), "Downloading"); }));
                    int count = dataGridView1.Rows.Count - 2;
                    this.Invoke(new MethodInvoker(delegate { dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows.Count - 1; }));
                    while (readCount > 0)
                    {
                        outputStream.Write(buffer, 0, readCount);
                        if (ftpStream.CanSeek)
                        {
                            long lll = ftpStream.Position;
                        }
                        readCount = ftpStream.Read(buffer, 0, bufferSize);
                    }
                    ftpStream.Close();
                    //outputStream.Close();
                    response.Close();
                    //this.Invoke(new MethodInvoker(delegate { dataGridView1.Rows[count].Cells[0].Value="Server1";}));
                    //this.Invoke(new MethodInvoker(delegate { dataGridView1.Rows[0].Cells[1].Value = getsubfile; }));
                    this.Invoke(new MethodInvoker(delegate { dataGridView1.Rows[count].Cells[2].Value = "Downloaded"; }));
                    this.Invoke(new MethodInvoker(delegate { dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.Rows.Count - 1; }));
                    //this.Invoke(new MethodInvoker(delegate { dataGridView1.Rows.Add("Server1", getsubfile.ToString(), "Downloaded"); }));
                }
                catch (WebException ex)
                {
                    string errMsg = (ex.Message);
                    fileQueue.Enqueue(fileName);
                    //MessageBox.Show("webexe"+ex.Message.ToString());

                }
                catch (Exception ex)
                {
                    string errMsg = (ex.Message);
                    MessageBox.Show("exe" + ex.Message.ToString());

                }
Posted
Updated 6-Dec-12 19:17pm
v2
Comments
lukeer 7-Dec-12 2:47am    
Don't repost[^]. Use the Improve question link beneath your original question instead.
Sampath Kumar Sathiya 31-Dec-12 1:25am    
You can check the size the file which is uploaded to ftp server and file in ftp server before download.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900