Slateboard
Slateboard

Reputation: 1031

Is Webclient unable to download .gif files?

Ok this is in C#, and I'm trying to download image files via url.

     System.Uri url = new Uri(attachments.FirstOrDefault().Url);
        string destination = @"E:\Pictures\Test\";
      using (WebClient client = new WebClient())
                    {
                        client.DownloadFile(url, destination);
                    }

So far, it works with .jpg, .jpeg, and .png files. But it does not work with .gif files. I am unsure of why.

Upvotes: 0

Views: 153

Answers (1)

Xavier
Xavier

Reputation: 1430

Using download file operates by looking at the ContentType in the response header, if you just want to download the file and save it with the same name that is used in the URL, then use DownloadData.

public static void Test()
{
    string urlString = "https://media0.giphy.com/media/o9ggk5IMcYlKE/200.gif";
    System.Uri url = new Uri(urlString);
    string destination = @"C:\Temp\";
    string[] urlSplit = urlString.Split("/");
    string filename = urlSplit[urlSplit.Length - 1];

    using (WebClient wc = new WebClient())
    {
        byte[] fileBytes = wc.DownloadData(url);
        System.IO.File.WriteAllBytes(destination + filename, fileBytes);
    }
}

Upvotes: 2

Related Questions