0

I have a camera connected to a network that provides images which can be accessed by IP. For example something like this.

http://170.1.2.3/image?camera=2

I get returned a jpg file, which I can easily display back on a webpage using normal html.

<img src="http://170.1.2.3/image?camera=2" />

But instead of simply showing the camera image as seen from the time of the page load, I also want to save this image locally to the server using an MVC4 application. In addition it would be nice to include a timestamp on the filename. I'm not having any luck finding an example that looks right to me.

Let's say I want to start with the most basic example:

string imgUrl = "http://170.1.2.3/image?camera=2";
savelocally(imgurl, "newfilename-"+DateTime.Now.ToString());

.. and savelocally() should place the file in "~/Uploads". How do I do this? All the examples I've come across have been for uploading a file from desktop. I don't know how to translate that into grabbing a remote image.

Update:

This is an incomplete answer, as pointed out by comments.

string url = "http://170.1.2.3/image?camera=2";
string localFilename = @"C:\Projects\MvcApplication1\MvcApplication1\Uploads\tofile.jpg";
using (WebClient client = new WebClient())
{
    client.DownloadFile(url, localFilename);
}

It works, however I don't like the fact that the file path is an absolute one. I tried replacing localFilename with ~/Uploads/tofile.jpg but it results in an error on execution. If possible I want to avoid having a fixed location for the upload folder.

0 Answers0