How to resume or pause the FTPWebRequest in C#.NET
-
Hi All, Iam using FTPWebRequest for downloading the files... Here, the function is working good. but while downloading the file, if the internet connection goes down, then this program going to throws the exception and end the process. So, in this scenario i want to pause/resume downloading... How to acheive this... If anybody knows help me... thanks Here my sample code ------------------- public void Download(string fileName) { //Desc: To download the file from the ftp server FtpWebRequest reqFTP; try { //fileName = <<Name of the file to be createdNeed not name on FTP server. name name()>> FileStream outputStream = new FileStream(downloadPath + "\\" + fileName, FileMode.Create); reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + ftpPath + "/" + 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); } reqFTP.Abort(); ftpStream.Close(); outputStream.Close(); response.Close(); } catch (Exception ex) { Utils.Write(ex.Message); } }
Thanks & Regards Kumaran
-
Hi All, Iam using FTPWebRequest for downloading the files... Here, the function is working good. but while downloading the file, if the internet connection goes down, then this program going to throws the exception and end the process. So, in this scenario i want to pause/resume downloading... How to acheive this... If anybody knows help me... thanks Here my sample code ------------------- public void Download(string fileName) { //Desc: To download the file from the ftp server FtpWebRequest reqFTP; try { //fileName = <<Name of the file to be createdNeed not name on FTP server. name name()>> FileStream outputStream = new FileStream(downloadPath + "\\" + fileName, FileMode.Create); reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + ftpPath + "/" + 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); } reqFTP.Abort(); ftpStream.Close(); outputStream.Close(); response.Close(); } catch (Exception ex) { Utils.Write(ex.Message); } }
Thanks & Regards Kumaran
You can't pause and resume the download, but you can start a new download and start it from a certain point. Have a look at the ContentOffset property of the FtpWebRequest object. Also you can find examples of this just by Googling for "ftpwebrequest resume download".
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008