2009-02-08 158 views
56

是否可以從Windows應用程序窗體中的網站下載文件並將其放入特定目錄?如何從C#網站下載文件?

+0

什麼交通工具? FTP? HTTP? – 2009-02-08 07:54:38

+0

米奇的評論是最直接,最準確的答案,哈哈! – Cerebrus 2009-02-08 07:56:50

+0

除非你是.net的新手,否則我會建議搜索MSDN文檔將有所幫助。尋找你想要達到的目標,在這個命名空間中可以適用,看看是否有一個類可以做到這一點:) – shahkalpesh 2009-02-08 08:08:10

回答

103

隨着WebClient class

using System.Net; 
//... 
WebClient Client = new WebClient(); 
Client.DownloadFile("http://i.stackoverflow.com/Content/Img/stackoverflow-logo-250.png", @"C:\folder\stackoverflowlogo.png"); 
72

使用WebClient.DownloadFile

using (WebClient client = new WebClient()) 
{ 
    client.DownloadFile("http://csharpindepth.com/Reviews.aspx", 
         @"c:\Users\Jon\Test\foo.txt"); 
} 
12

當然,你只需要使用一個HttpWebRequest

一旦你的HttpWebRequest設置,您可以響應流(根據MIME類型無論是BinaryWriter,或者TextWriter)保存到一個文件StreamWriter,你有你的硬盤驅動器上的文件。

編輯:忘了WebClient。除非您只需要使用GET來檢索您的文件,否則這種方式效果不錯。如果該網站要求您提供POST信息,則必須使用HttpWebRequest,所以我要離開我的答案。

0

試試這個例子:

public void TheDownload(string path) 
{ 
    System.IO.FileInfo toDownload = new System.IO.FileInfo(HttpContext.Current.Server.MapPath(path)); 

    HttpContext.Current.Response.Clear(); 
    HttpContext.Current.Response.AddHeader("Content-Disposition", 
      "attachment; filename=" + toDownload.Name); 
    HttpContext.Current.Response.AddHeader("Content-Length", 
      toDownload.Length.ToString()); 
    HttpContext.Current.Response.ContentType = "application/octet-stream"; 
    HttpContext.Current.Response.WriteFile(patch); 
    HttpContext.Current.Response.End(); 
} 

的實現是在如下進行:

TheDownload("@"c:\Temporal\Test.txt""); 

來源:http://www.systemdeveloper.info/2014/03/force-downloading-file-from-c.html

2

您可以使用此代碼從一個網站下載文件到桌面:

12

在發出請求之前,您可能需要知道文件下載期間的狀態或使用憑據。

下面是覆蓋這些選項的例子:

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

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

    client.DownloadProgressChanged += WebClientDownloadProgressChanged; 
    client.DownloadDataCompleted += WebClientDownloadCompleted; 
    client.DownloadFileAsync(ur, @"C:\path\newImage.jpg"); 
} 

並實施了回調的功能如下:

void WebClientDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) 
{ 
    Console.WriteLine("Download status: {0}%.", e.ProgressPercentage); 

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

void WebClientDownloadCompleted(object sender, DownloadDataCompletedEventArgs e) 
{ 
    Console.WriteLine("Download finished!"); 
} 

LAMBDA符號:用於處理事件

其他可能的選項
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(delegate(object sender, DownloadProgressChangedEventArgs e) { 
    Console.WriteLine("Download status: {0}%.", e.ProgressPercentage); 

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

client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(delegate(object sender, DownloadDataCompletedEventArgs e){ 
    Console.WriteLine("Download finished!"); 
}); 

我們可以做的更好

client.DownloadProgressChanged += (object sender, DownloadProgressChangedEventArgs e) => 
{ 
    Console.WriteLine("Download status: {0}%.", e.ProgressPercentage); 

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

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

或者

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