wayofthefuture
wayofthefuture

Reputation: 9425

Simple way to show image being continually updated via FTP

We have an IP camera that takes snapshots and uploads the file via FTP to our server on a 5 minute interval. It uploads to the same file name every time, i.e. snapshot.jpg. We show the file on the webpage like so:

<img src="snapshot.jpg" style="width:100%;">

The problem is, when the image is in the middle of uploading, it does not show on the web page and instead shows the broken image icon. It would seem there is no way to use this image as while it's uploading it's not accessible yet. Here is our quick fix, which is to detect a file size of greater than 0 (theoretically its in the middle of uploading), that doesn't work quite right and as you can see will result in the image not showing at all:

            if (File.Exists(path + co.filename))
            {
                FileInfo fo = new FileInfo(path + co.filename);

                System.DateTime lastwrite = fo.LastWriteTime;
                decimal minold = Convert.ToDecimal(Math.Round(DateTime.Now.Subtract(lastwrite).TotalMinutes, 1));

                if (fo.Length > 1000 && minold < 15)
                {
                    System.DateTime lastwritecst = lastwrite.AddHours(-1);

                    pl.Controls.Add(new LiteralControl("<span class=\"blacktextmedium titletext\">" + co.name + "<br>&nabla; " + lastwritecst.ToString("HH:mm") + " - " + Convert.ToString(minold) + " min &nabla;</span><br>"));
                    pl.Controls.Add(new LiteralControl(GetImgHtml(url + co.filename)));
                    pl.Controls.Add(new LiteralControl("<br>"));
                }
            }

Does anyone have any idea of how to circumvent the problem while the image is in the middle of uploading, or any ideas of a better way to do it? We are using C#.NET on the server side.

Upvotes: 0

Views: 183

Answers (1)

Blindy
Blindy

Reputation: 67417

Don't overwrite the "live" image, save it as a .tmp file and rename it when it's done.

Upvotes: 1

Related Questions