How to download ftp files with startup

I have a problem, the problem is that I can upload ftp files, but the files I downoad do not have the ability to resume and a lot of file files, because there is a large file larger than 500 MB of files, I can’t download files constantly, because I get disconnect and start the download from the beginning, I wanted my code to resume working if it disconnects

The code I use

public string[] GetFileList() { string[] downloadFiles; StringBuilder result = new StringBuilder(); FtpWebRequest reqFTP; try { reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/")); reqFTP.UseBinary = true; reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword); reqFTP.Method = WebRequestMethods.Ftp.ListDirectory; WebResponse response = reqFTP.GetResponse(); StreamReader reader = new StreamReader(response.GetResponseStream()); //MessageBox.Show(reader.ReadToEnd()); string line = reader.ReadLine(); while (line != null) { result.Append(line); result.Append("\n"); line = reader.ReadLine(); } result.Remove(result.ToString().LastIndexOf('\n'), 1); reader.Close(); response.Close(); //MessageBox.Show(response.StatusDescription); return result.ToString().Split('\n'); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); downloadFiles = null; return downloadFiles; } } private void Download(string filePath, string fileName) { FtpWebRequest reqFTP; try { //filePath = <<The full path where the file is to be created.>>, //fileName = <<Name of the file to be created(Need not be the name of the file on FTP server).>> FileStream outputStream = new FileStream(filePath + "\\" + fileName, FileMode.Create); reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + 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; int bufferSize = 2048; int readCount; byte[] buffer = new byte[bufferSize]; readCount = ftpStream.Read(buffer, 0, bufferSize); while (readCount > 0) { outputStream.Write(buffer, 0, readCount); readCount = ftpStream.Read(buffer, 0, bufferSize); } ftpStream.Close(); outputStream.Close(); response.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } } 

private void btnDownload_Click (object sender, EventArgs e) {

  FolderBrowserDialog fldDlg = new FolderBrowserDialog(); if (txtUpload.Text.Trim().Length > 0) { if (fldDlg.ShowDialog() == DialogResult.OK) { Download(fldDlg.SelectedPath, txtUpload.Text.Trim()); } } else { MessageBox.Show("Please enter the File name to download"); } } 

I need a resume mechanism in my code,

would be an excellent assessment if someone could help me,

Thanks in advance

+4
source share
1 answer

Before starting the download, check for the file on the local file system. If it exists, get the size and use it for the ContentOffset member of the FtpWebRequest object. However, this function may not be supported by the FTP server.

+6
source

All Articles