简体   繁体   中英

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.

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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM