3

I'm required to retrieve and save an image from a website to my local folder. The image type varies between .png, .jpg and .gif

I've tried using

string url = @"http://redsox.tcs.auckland.ac.nz/CSS/CSService.svc/";
string saveLoc = @"/project1/home_image";
using (var wc = new WebClient())
{
    wc.DownloadFile(url, saveLoc);
}

but this saves the file 'home_image' in the folder without the extension. My question is how do you determine the extension? Is there a simple way to do this? Can one use the Content-Type of the HTTP request? If so, how do you do this?

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Seb W
  • 521
  • 2
  • 6
  • 18
  • You need to give the file name with the path `@"/project1/home_image/Someimage.png"` – Nilesh Aug 31 '13 at 01:37
  • I don't know the extension though, that's what I need to find out so that I can save it with the correct one. – Seb W Aug 31 '13 at 01:42
  • You need to get mime type from response's headers, map it to extension and use it. Unfortunately `WebClient` is too high level method to give you access to headers. – Alexei Levenkov Aug 31 '13 at 01:47
  • You need to use `HttpWebRequest` in that case. But my question is if you know the image that you need to download you might as well know the extension! Does your service method stream an image? – Nilesh Aug 31 '13 at 01:48

2 Answers2

10

If you want to use a WebClient, then you have to extract the header information from WebClient.ResponseHeaders. You'll have to store it as a byte array first, and then save the file after getting your file information.

string url = @"http://redsox.tcs.auckland.ac.nz/CSS/CSService.svc/";
string saveLoc = @"/project1/home_image";

using (WebClient wc = new WebClient())
{
    byte[] fileBytes = wc.DownloadData(url);

    string fileType = wc.ResponseHeaders[HttpResponseHeader.ContentType];

    if (fileType != null)
    {
        switch (fileType)
        {
            case "image/jpeg":
                saveloc += ".jpg";
                break;
            case "image/gif":
                saveloc += ".gif";
                break;
            case "image/png":
                saveloc += ".png";
                break;
            default:
                break;
        }

        System.IO.File.WriteAllBytes(saveloc, fileBytes);
    }
}

I like my extensions to be 3 letters long if they can.... personal preference. If it doesn't bother you, you can replace the entire switch statement with:

saveloc += "." + fileType.Substring(fileType.IndexOf('/') + 1);

Makes the code a little neater.

Ichabod Clay
  • 1,981
  • 13
  • 17
  • for ppm files the mime type is image/x-portable-pixmap, so the 2nd option will produce a ".x-portable-pixmap" file, which probably won't be associated with your image program. – satibel Dec 15 '16 at 12:31
0

Try something like this

 HttpWebRequest request = (HttpWebRequest)WebRequest.Create("Your URL");
 request.Method = "GET";
 var response = request.GetResponse();
 var contenttype = response.Headers["Content-Type"]; //Get the content type and extract the extension.
 var stream = response.GetResponseStream();

Then save the stream

Nilesh
  • 2,583
  • 5
  • 21
  • 34