Hi namojainashis...,
Refer below code.
Namespaces
C#
using System.IO;
using System.Net;
VB.Net
Imports System.IO
Imports System.Net
Code
C#
protected void DownloadFile(object sender, EventArgs e)
{
string ftp = "ftp://15.16.45.54/";
string ftpFolder = "UploadToDMS/132322/";
string fileName = "132322_AE_IE_SC Contract-V01-cgip.pdf";
try
{
//Create FTP Request.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
request.Method = WebRequestMethods.Ftp.DownloadFile;
//Enter FTP Server credentials.
request.Credentials = new NetworkCredential("Username", "Password");
request.UsePassive = true;
request.UseBinary = true;
request.EnableSsl = false;
//Fetch the Response and read it into a MemoryStream object.
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
using (MemoryStream stream = new MemoryStream())
{
//Download the File.
response.GetResponseStream().CopyTo(stream);
Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(stream.ToArray());
Response.End();
}
}
catch (WebException ex)
{
throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
}
}
VB.Net
Protected Sub DownloadFile(sender As Object, e As EventArgs)
Dim ftp As String = "ftp://15.16.45.54/"
Dim ftpFolder As String = "UploadToDMS/132322/"
Dim fileName As String = "132322_AE_IE_SC Contract-V01-cgip.pdf"
Try
'Create FTP Request.
Dim request As FtpWebRequest = DirectCast(WebRequest.Create(Convert.ToString(ftp & ftpFolder) & fileName), FtpWebRequest)
request.Method = WebRequestMethods.Ftp.DownloadFile
'Enter FTP Server credentials.
request.Credentials = New NetworkCredential("Username", "Password")
request.UsePassive = True
request.UseBinary = True
request.EnableSsl = False
'Fetch the Response and read it into a MemoryStream object.
Dim resp As FtpWebResponse = DirectCast(request.GetResponse(), FtpWebResponse)
Using stream As New MemoryStream()
'Download the File.
resp.GetResponseStream().CopyTo(stream)
Response.AddHeader("content-disposition", "attachment;filename=" & fileName)
Response.Cache.SetCacheability(HttpCacheability.NoCache)
Response.BinaryWrite(stream.ToArray())
Response.End()
End Using
Catch ex As WebException
Throw New Exception(TryCast(ex.Response, FtpWebResponse).StatusDescription)
End Try
End Sub
For more details refer below article.