2017-05-20 4 views
0

목록 상자에 다음과 같은 링크가 가득 찼습니다 : http://example.com/sorted/Avicii/Avicii+-+Wake+Me+Up.mp3 그리고 나는 그들을 모두 같은 시간에 다운로드하고 싶습니까? 방법같은 시간에 여러 파일 내려 받기

당신이 윈폼 또는 ASP.NET 작업하는 경우 확실 해요
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Net; 

namespace SillyCSharpNameSpace 
{ 
    public class VerboseDownloaderClassName 
    { 
     private string downloadFile(string url, string localDir) 
     { 
      try 
      { 
       var localPath = Path.Combine(localDir, Path.GetFileName(url)); 
       using (var wc = new WebClient()) 
       { 
        wc.DownloadFile(url, localPath); 
        return localPath; 
       } 
      } 
      catch (Exception e) 
      { 
       Console.WriteLine(e); 
       return null; 
      } 
     } 

     public void DownloadAll(List<string> urls) 
     { 
      urls.AsParallel() 
       .Where(url => !string.IsNullOrWhiteSpace(url)) 
       .WithDegreeOfParallelism(20) 
       .Select(url => downloadFile(url, ".")); 
     } 
    } 
} 

당신이 알아낼 수 : 이 코드는 I는 다음과 같이 할 수있는 하나의 파일

private void txtUrl_TextChanged(object sender, EventArgs e) 
    { 
     try 
     { 
      // function that enter the file name automatically in the savefiledialog. 
      Uri uri = new Uri(txtUrl.Text); 
      // Save the file name to the string. 
      filename = Path.GetFileName(uri.LocalPath); 
     } 
     catch 
     { 
      // no need need an exception message. 
     } 
    } 
    private void DownloadFile(string url, string save) 
    { 
     using (var client = new WebClient()) 
     { 
      // Run code every time the download changes. 
      client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(Changed); 
      // Run codes when file download has been completed. 
      client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed); 
          client.DownloadFileAsync(new Uri(url), save); 
+0

첫 번째 제안 : 지금까지 시도한 코드를 게시하십시오. 두 번째 제안 : 정확히 어디에서 붙어 있는지 설명하십시오. – DigiFriend

+0

나는 하나의 파일을 다운로드하는 방법이 있지만 실용적인 @DigiFriend을 가지고 있지 않다. – Elmissouri

+0

루프는 당신 친구 야. 비동기 메소드와 같습니다. – DigiFriend

답변

1

에게 솔루션을 다운로드하는 데 사용 목록 상자 항목에서 URL 문자열을 가져옵니다. Path.GetFileName()은 URL 매개 변수가없는 URL 끝에 파일 이름과 함께 제공 한 양식에 대해서만 작동합니다. AsParallel 메서드는 다운로드 작업을 20 개의 "스레드"로 병렬 처리합니다. 나는 그것이 당신의 목적을 위해 충분해야한다고 생각합니다.

보너스. F #에서와 동일합니다. o)

open System.IO 
open System.Net 
open FSharp.Collections.ParallelSeq // from NuGet FSharp.Collections.ParallelSeq 

let downloadFile dir url = 
    let localPath = Path.Combine(dir, Path.GetFileName url) 
    try 
     use wc = new WebClient() 
     wc.DownloadFile(url, localPath) 
     Some localPath 
    with ex -> 
     printfn "Can't download file from %s: %A" url ex 
     None 

let downloadAll (urls: string list) = 
    urls 
    |> PSeq.withDegreeOfParallelism 20 // 20 threads 
    |> PSeq.choose (downloadFile ".") 
+0

고마워요, 도와 주셔서 감사합니다. – Elmissouri

+0

환영합니다. 여기 stackoverflow 그들은 대답을 수락 도움을 주셔서 감사합니다, O) –

+0

그냥 내가 멀리 있었는지 확실한 물건 – Elmissouri

관련 문제