简体   繁体   English

Webclient 无法下载 .gif 文件吗?

[英]Is Webclient unable to download .gif files?

Ok this is in C#, and I'm trying to download image files via url.好的,这是在 C# 中,我正在尝试通过 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.到目前为止,它适用于 .jpg、.jpeg 和 .png 文件。 But it does not work with .gif files.但它不适用于 .gif 文件。 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.使用下载文件通过查看响应头中的 ContentType 进行操作,如果您只想下载文件并使用 URL 中使用的相同名称保存它,则使用 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);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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