5 Comments
  •   Posted in: 
  • C#

At the office we have a Conceptronic CnetCam2. I have configured this IP Webcam to log every x seconds to an FTP Server. But now I would like to view the taken snapshot images in my web-application. The webcam makes this directory structure:

filezilla

So every day is a new folder and has the format of ‘hhMMss_x.jpg’ (h = hour, M = minutes, s = seconds, x= random?) So my Asp.Net web application has to connect through FTP to extract the latest jpg and display it. I have found this class on CodeProject for setting up an FTP connection. I have uploaded it to this blog as a mirror FtpClientCS.zip (6.62 kb) All credits for that class go to Howard Richards. So after I have added the C# class to the project I have made the following code. [more] Which is awesome because of the IEnumerable interface.

private void GetScreenCap(string hostname, string username, string password)
{
    try
    {
        FTPclient fc        = new FTPclient(hostname, username, password);
        FTPdirectory dir    = fc.ListDirectoryDetail("/cam/");
        FTPfileInfo dirLast = dir.GetDirectories().Last();
        FTPfileInfo fi      = fc.ListDirectoryDetail(dirLast.FullName + "/").GetFiles("jpg").Last();

        fc.Download(fi.FullName, Server.MapPath("~/tmpwrite/" + fi.Filename), true);

        Image img = new Image();
        string alt= "Webcam image: " + fi.Filename.Replace(".jpg", "");
        img.AlternateText = alt;
        img.ImageUrl = "~/tmpwrite/" + fi.Filename;
        ph.Controls.Add(img);
    }
    catch (Exception ex)
    {
        Literal li = new Literal();
        li.Text = ex.Message + "<br/>" + ex.StackTrace;
        ph.Controls.Add(li);
    }
}
 

As you can see, is the .Last method very handy to get the latest file from the latest directory. The rest is just boring stuff to save the file to the disk and make an Image object and point to the download location.

So use & share etc. Good luck! Open-mouthed smile

kick it on DotNetKicks.com Shout it

Pin on pinterest Plus on Googleplus Post on LinkedIn

Comments

Comment by DotNetKicks.com

FTP with Asp.Net C

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Comment by DotNetShoutout

FTP with Asp.Net C

Thank you for submitting this cool story - Trackback from DotNetShoutout

Comment by narayana mora

hi

i uploaded images with file upload control and saved in to ftp aswell as db but when i retrive that images not coming but all fields are comming except image field means image path is the field name

any one the solution plz help me

narayana mora
Comment by JP Hellemons

@narayana mora please post some code on stackoverflow, send me the link and I will take a look!

JP Hellemons
Comment by Sergio

Thank this class helped me a lot of what I needed to do in the system

Sergio