C #의 URL에서 파일을 다운로드하는 방법은 무엇입니까?


350

URL 경로에서 파일을 다운로드하는 간단한 방법은 무엇입니까?


13
System.Net.WebClient
— seanb

답변:


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 # 코드가 웹 서버에서 실행되는 경우 위험한 것입니다.
— Heinzi

177

이 네임 스페이스를 포함

using System.Net;

비동기 적으로 다운로드 하고 ProgressBar 를 넣어 UI Thread 자체 내에서 다운로드 상태를 표시하십시오.

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
질문은 가장 간단한 방법을 요구합니다. 더 복잡하게 만드는 것이 가장 단순하지는 않습니다.
— Enigmativity

75
대부분의 사람들은 다운로드하는 동안 진행률 표시 줄을 선호합니다. 그래서 나는 그것을하는 가장 간단한 방법을 썼습니다. 이것은 대답이 아닐 수도 있지만 Stackoverflow의 요구 사항을 충족합니다. 그것은 누군가를 돕는 것입니다.
— Sayka

3
진행률 표시 줄을 그대로두면 다른 대답만큼 간단합니다. 이 답변에는 네임 스페이스도 포함되며 I / O에 비동기를 사용합니다. 또한 질문은 가장 간단한 방법을 요구하지 않고 단순한 방법을 요구합니다. :)
— Josh

2 개의 답변을 하나만주는 것이 좋으며 진행률 표시 줄이있는 것이 더 낫습니다.
— Jesse de gans

@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의 의견에서 내 대답을 찾았지만 다른 사람들보다이 "저품질"답변을 선호합니다. 완전하고 (명명 서 사용) 간결하고 이해하기 쉽습니다. IMHO, 오래된 질문은 관련이 없습니다.
— Josh

21
그러나 WebClient는 사용한 후에 폐기해야한다고 생각하기 때문에 Using의 대답이 훨씬 낫습니다. 그것을 사용하여 내부에 넣으면 폐기됩니다.
— Ricardo Polo Jaramillo

5
그것은 ... using 문을 여기에 그냥 ... 웹 클라이언트가 처분을 할 사용으로 사용하지 어떤 것을 사용하는 네임 스페이스를 보여이 코드 예제 폐기 함께 할 수 없다
— 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

9

또한 WebClient 클래스에서 DownloadFileAsync 메서드를 사용할 수 있습니다. 지정된 URI를 가진 리소스를 로컬 파일로 다운로드합니다. 또한이 메소드는 호출 스레드를 차단하지 않습니다.

견본:

    webClient.DownloadFileAsync(new Uri("http://www.example.com/file/test.jpg"), "test.jpg");

자세한 내용은:

http://csharpexamples.com/download-files-synchronous-asynchronous-url-c/


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

내 경험으로 as를 사용 하지 않는 것이 GetIsNetworkAvailable()너무 많은 오 탐지를 반환 한다고 제안 합니다.
— 체로 나

LAN과 같은 컴퓨터 네트워크에 있지 않으면 GetIsNetworkAvailable()항상 올바르게 반환됩니다. 이 경우 System.Net.WebClient().OpenRead(Uri)메소드를 사용 하여 기본 URL이 제공 될 때 리턴되는지 확인할 수 있습니다 .
— 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를 업데이트해야 할 수도 있습니다.

다음은 이러한 옵션을 다루는 예입니다. 람다 표기법 과 문자열 보간법 이 사용되었습니다.

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을 사용하면 프로젝트 디렉토리의 폴더 안에 다운로드됩니다 . 위 코드의 기능을 이해하려면 코드의 주석을 읽으십시오.

당사 사이트를 사용함과 동시에 당사의 쿠키 정책과 개인정보 보호정책을 읽고 이해하였음을 인정하는 것으로 간주합니다.
Licensed under cc by-sa 3.0 with attribution required.