2015-12-21 49 views
0

這是我下載的代碼(從計算器所),一個固定的路徑上查看和存儲圖像:如何下載並保存兩次圖像?

private static void DownloadRemoteImageFile(string uri, string path) 
{ 
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); 
    HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 

    if ((response.StatusCode == HttpStatusCode.OK || 
     response.StatusCode == HttpStatusCode.Moved || 
     response.StatusCode == HttpStatusCode.Redirect) && 
     response.ContentType.StartsWith("image", StringComparison.OrdinalIgnoreCase)) 
    { 
     using (Stream inputStream = response.GetResponseStream()) 
     using (Stream outputStream = File.OpenWrite(path)) 
     { 
      byte[] buffer = new byte[4096]; 
      int bytesRead; 
      do 
      { 
       bytesRead = inputStream.Read(buffer, 0, buffer.Length); 
       outputStream.Write(buffer, 0, bytesRead); 
      } while (bytesRead != 0); 
     } 
    } 
} 

現在,讓我們說,我倒是創建兩個pathpath + "\duplicates\";圖像:什麼最好的辦法呢?

回想起不同路徑的功能(所以我需要下載兩次)或者我可以使用多個outputStream

+0

您可以簡單地使用多個outputStreams – Jaco

+1

您的目標是雙倍下載數據作爲對數據的雙重檢查,或者您是否願意只下載一次然後在本地複製它? – adv12

回答

3

你的循環後,只需複製文件是這樣的:

if (File.Exists(path)) 
    File.Copy(path, duplicatePath); 
5

System.Io.File.Copy(路徑,duplicatePath)

2

你可以簡單地使用多個outputStreams這樣的:

using (Stream inputStream = response.GetResponseStream()) 
using (Stream outputStream = File.OpenWrite(path)) 
using (Stream outputStream2 = File.OpenWrite(duplicatePath)) 
{ 
    byte[] buffer = new byte[4096]; 
    int bytesRead; 
    do 
    { 
     bytesRead = inputStream.Read(buffer, 0, buffer.Length); 
     outputStream.Write(buffer, 0, bytesRead); 
     outputStream2.Write(buffer, 0, bytesRead); 
    } while (bytesRead != 0); 
}