如何从C#中的URL下载文件?


350

从URL路径下载文件的简单方法是什么?


13
看看System.Net.WebClient
seanb

Answers:


475
using (var client = new WebClient())
{
    client.DownloadFile("http://example.com/file/song/a.mpeg", "a.mpeg");
}

24
有史以来最好的解决方案,但我想添加1条重要行“ client.Credentials = new NetworkCredential(“ UserName”,“ Password”);'
开发人员

3
令人欢迎的副作用:此方法还支持将本地文件作为第一个参数
oo_dev

MSDN文档确实提到现在改为使用HttpClient:docs.microsoft.com/en-us/dotnet/api/…–
StormsEngineering

尽管我认为WebClient似乎是一个更加直接和简单的解决方案。
StormsEngineering

1
@ copa017:或者是危险的,例如,URL是用户提供的,并且C#代码在Web服务器上运行。
Heinzi

177

包括这个名称空间

using System.Net;

异步下载并放置一个ProgressBar以在UI线程自身中显示下载状态

private void BtnDownload_Click(object sender, RoutedEventArgs e)
{
    using (WebClient wc = new WebClient())
    {
        wc.DownloadProgressChanged += wc_DownloadProgressChanged;
        wc.DownloadFileAsync (
            // Param1 = Link of file
            new System.Uri("http://www.sayka.com/downloads/front_view.jpg"),
            // Param2 = Path to save
            "D:\\Images\\front_view.jpg"
        );
    }
}
// Event to track the progress
void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    progressBar.Value = e.ProgressPercentage;
}

14
问题要求最简单的方法。使变得更复杂并不是使它最简单。

75
大多数人在下载时都会喜欢进度条。所以我只写了最简单的方法。这可能不是答案,但它满足Stackoverflow的要求。那是为了帮助某人。
赛卡2015年

3
如果您只忽略进度条,这与其他答案一样简单。此答案还包括名称空间,并将异步用于I / O。问题也不是要求最简单的方法,而只是简单的方法。:)
乔什(Josh)

我认为给2个答案一个简单的答案,
再给

@Jessedegans已经有一个答案,表明如何无需进度条即可简单地下载。那就是为什么我写了一个有助于异步下载和进度条实现的答案
Sayka

75

用途System.Net.WebClient.DownloadFile

string remoteUri = "http://www.contoso.com/library/homepage/images/";
string fileName = "ms-banner.gif", myStringWebResource = null;

// Create a new WebClient instance.
using (WebClient myWebClient = new WebClient())
{
    myStringWebResource = remoteUri + fileName;
    // Download the Web resource and save it into the current filesystem folder.
    myWebClient.DownloadFile(myStringWebResource, fileName);        
}

42
using System.Net;

WebClient webClient = new WebClient();
webClient.DownloadFile("http://mysite.com/myfile.txt", @"c:\myfile.txt");

33
欢迎来到SO!通常,将低质量的答案发布到已经高度评价答案的现有和旧问题上不是一个好主意。
ThiefMaster

28
我从seanb的评论中找到了答案,但实际上,我更喜欢这个“低质量”的答案。它是完整的(使用语句),简洁且易于理解。恕我直言,作为一个老问题是无关紧要的。
2013年

21
但是它认为使用Using的答案要好得多,因为,我认为WebClient应该在使用后丢弃。使用将其放入内部可确保将其丢弃。
里卡多·波罗·贾拉米洛

5
在此代码示例中,它与处理无关...此处的using语句仅显示要使用的名称空间,而对WebClient用于处理没有使用...
cdie

17

完成类以在将状态打印到控制台时下载文件。

using System;
using System.ComponentModel;
using System.IO;
using System.Net;
using System.Threading;

class FileDownloader
{
    private readonly string _url;
    private readonly string _fullPathWhereToSave;
    private bool _result = false;
    private readonly SemaphoreSlim _semaphore = new SemaphoreSlim(0);

    public FileDownloader(string url, string fullPathWhereToSave)
    {
        if (string.IsNullOrEmpty(url)) throw new ArgumentNullException("url");
        if (string.IsNullOrEmpty(fullPathWhereToSave)) throw new ArgumentNullException("fullPathWhereToSave");

        this._url = url;
        this._fullPathWhereToSave = fullPathWhereToSave;
    }

    public bool StartDownload(int timeout)
    {
        try
        {
            System.IO.Directory.CreateDirectory(Path.GetDirectoryName(_fullPathWhereToSave));

            if (File.Exists(_fullPathWhereToSave))
            {
                File.Delete(_fullPathWhereToSave);
            }
            using (WebClient client = new WebClient())
            {
                var ur = new Uri(_url);
                // client.Credentials = new NetworkCredential("username", "password");
                client.DownloadProgressChanged += WebClientDownloadProgressChanged;
                client.DownloadFileCompleted += WebClientDownloadCompleted;
                Console.WriteLine(@"Downloading file:");
                client.DownloadFileAsync(ur, _fullPathWhereToSave);
                _semaphore.Wait(timeout);
                return _result && File.Exists(_fullPathWhereToSave);
            }
        }
        catch (Exception e)
        {
            Console.WriteLine("Was not able to download file!");
            Console.Write(e);
            return false;
        }
        finally
        {
            this._semaphore.Dispose();
        }
    }

    private void WebClientDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        Console.Write("\r     -->    {0}%.", e.ProgressPercentage);
    }

    private void WebClientDownloadCompleted(object sender, AsyncCompletedEventArgs args)
    {
        _result = !args.Cancelled;
        if (!_result)
        {
            Console.Write(args.Error.ToString());
        }
        Console.WriteLine(Environment.NewLine + "Download finished!");
        _semaphore.Release();
    }

    public static bool DownloadFile(string url, string fullPathWhereToSave, int timeoutInMilliSec)
    {
        return new FileDownloader(url, fullPathWhereToSave).StartDownload(timeoutInMilliSec);
    }
}

用法:

static void Main(string[] args)
{
    var success = FileDownloader.DownloadFile(fileUrl, fullPathWhereToSave, timeoutInMilliSec);
    Console.WriteLine("Done  - success: " + success);
    Console.ReadLine();
}

1
请您能解释一下为什么要SemaphoreSlim在这种情况下使用吗?
mmushtaq

10

尝试使用此:

private void downloadFile(string url)
{
     string file = System.IO.Path.GetFileName(url);
     WebClient cln = new WebClient();
     cln.DownloadFile(url, file);
}

文件将保存在哪里?
IB

该文件将保存在可执行文件所在的位置。如果您想使用完整路径,请使用完整路径以及文件(这是要下载的项目的文件名)
Surendra Shrestha


8

使用GetIsNetworkAvailable()来检查网络连接,以避免在未连接到网络时创建空文件。

if (System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
{
    using (System.Net.WebClient client = new System.Net.WebClient())
    {                        
          client.DownloadFileAsync(new Uri("http://www.examplesite.com/test.txt"),
          "D:\\test.txt");
    }                  
}

我建议不要使用,GetIsNetworkAvailable()因为根据我的经验,它会返回太多的假阳性。
Cherona

除非您位于局域网之类的计算机网络中,否则GetIsNetworkAvailable()始终可以正确返回。在这种情况下,您可以使用System.Net.WebClient().OpenRead(Uri)方法查看在给定默认URL时是否返回。参见WebClient.OpenRead()
haZya

2

下面的代码包含使用原始名称下载文件的逻辑

private string DownloadFile(string url)
    {

        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
        string filename = "";
        string destinationpath = Environment;
        if (!Directory.Exists(destinationpath))
        {
            Directory.CreateDirectory(destinationpath);
        }
        using (HttpWebResponse response = (HttpWebResponse)request.GetResponseAsync().Result)
        {
            string path = response.Headers["Content-Disposition"];
            if (string.IsNullOrWhiteSpace(path))
            {
                var uri = new Uri(url);
                filename = Path.GetFileName(uri.LocalPath);
            }
            else
            {
                ContentDisposition contentDisposition = new ContentDisposition(path);
                filename = contentDisposition.FileName;

            }

            var responseStream = response.GetResponseStream();
            using (var fileStream = File.Create(System.IO.Path.Combine(destinationpath, filename)))
            {
                responseStream.CopyTo(fileStream);
            }
        }

        return Path.Combine(destinationpath, filename);
    }

1

在进行文件下载之前,您可能需要了解状态并更新ProgressBar,或者使用凭据进行请求。

这是一个涵盖这些选项的示例。使用了Lambda表示法字符串插值

using System.Net;
// ...

using (WebClient client = new WebClient()) {
    Uri ur = new Uri("http://remotehost.do/images/img.jpg");

    //client.Credentials = new NetworkCredential("username", "password");
    String credentials = Convert.ToBase64String(Encoding.ASCII.GetBytes("Username" + ":" + "MyNewPassword"));
    client.Headers[HttpRequestHeader.Authorization] = $"Basic {credentials}";

    client.DownloadProgressChanged += (o, e) =>
    {
        Console.WriteLine($"Download status: {e.ProgressPercentage}%.");

        // updating the UI
        Dispatcher.Invoke(() => {
            progressBar.Value = e.ProgressPercentage;
        });
    };

    client.DownloadDataCompleted += (o, e) => 
    {
        Console.WriteLine("Download finished!");
    };

    client.DownloadFileAsync(ur, @"C:\path\newImage.jpg");
}

1

根据我的研究,我发现这WebClient.DownloadFileAsync是下载文件的最佳方法。它在System.Net命名空间中可用,并且还支持.net核心。

这是下载文件的示例代码。

using System;
using System.IO;
using System.Net;
using System.ComponentModel;

public class Program
{
    public static void Main()
    {
        new Program().Download("ftp://localhost/test.zip");
    }
    public void Download(string remoteUri)
    {
        string FilePath = Directory.GetCurrentDirectory() + "/tepdownload/" + Path.GetFileName(remoteUri); // path where download file to be saved, with filename, here I have taken file name from supplied remote url
        using (WebClient client = new WebClient())
        {
            try
            {
                if (!Directory.Exists("tepdownload"))
                {
                    Directory.CreateDirectory("tepdownload");
                }
                Uri uri = new Uri(remoteUri);
                //password username of your file server eg. ftp username and password
                client.Credentials = new NetworkCredential("username", "password");
                //delegate method, which will be called after file download has been complete.
                client.DownloadFileCompleted += new AsyncCompletedEventHandler(Extract);
                //delegate method for progress notification handler.
                client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgessChanged);
                // uri is the remote url where filed needs to be downloaded, and FilePath is the location where file to be saved
                client.DownloadFileAsync(uri, FilePath);
            }
            catch (Exception)
            {
                throw;
            }
        }
    }
    public void Extract(object sender, AsyncCompletedEventArgs e)
    {
        Console.WriteLine("File has been downloaded.");
    }
    public void ProgessChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        Console.WriteLine($"Download status: {e.ProgressPercentage}%.");
    }
}

带有上面的代码文件将被下载到里面 tepdownload到项目目录的文件夹中。请阅读代码中的注释以了解上述代码的作用。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.