Retrieve and display Image from FTP folder using C# and VB.Net in ASP.Net

Last Reply 27 days ago By dharmendr

Posted 28 days ago

I have store the image in FTP folder but dont know how to retrieve the image from the folder and display in my webpage..

Please give me some ideas and lead me to finish this task


Posted 28 days ago

Hi Sir,

i want to get image from my FTP folder and want to display in my browser page


Posted 27 days ago

Hi Dev@KKM,

Check with the below code.

C#

protected void Page_Load(object sender, EventArgs e)
{
    //FTP Server URL.
    string ftp = "ftp://yourserver.com/";

    //FTP Folder name. Leave blank if you want to list files from root folder.
    string ftpFolder = "Uploads/";
    try
    {
        string fileName = "Desert.jpg";
        //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())
        {
            response.GetResponseStream().CopyTo(stream);
            string base64String = Convert.ToBase64String(stream.ToArray(), 0, stream.ToArray().Length);
            Image1.ImageUrl = "data:image/png;base64," + base64String;
        }          
    }
    catch (WebException ex)
    {
        throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    'FTP Server URL.
    Dim ftp As String = "ftp://yourserver.com/"
    'FTP Folder name. Leave blank if you want to list files from root folder.
    Dim ftpFolder As String = "Uploads/"
    Try
        Dim fileName As String = "Desert.jpg"
        'Create FTP Request.
        Dim request As FtpWebRequest = CType(WebRequest.Create(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 response As FtpWebResponse = CType(request.GetResponse(), FtpWebResponse)
        Using stream As MemoryStream = New MemoryStream()
            response.GetResponseStream().CopyTo(stream)
            Dim base64String As String = Convert.ToBase64String(stream.ToArray(), 0, stream.ToArray().Length)
            Image1.ImageUrl = "data:image/png;base64," & base64String
        End Using
    Catch ex As WebException
        Throw New Exception((TryCast(ex.Response, FtpWebResponse)).StatusDescription)
    End Try
End Sub