2016-06-21 61 views
0

我有一個文件不會下載的問題,即使它顯示爲已完成。即使它說它沒有下載文件

該文件沒有顯示在它應該被下載到的位置。

這是我的代碼:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 
{ 
    if(e.ColumnIndex == 2) 
    { 
     int rowIndex = e.RowIndex; 
     DataGridViewRow row = dataGridView1.Rows[rowIndex]; 
     string value1 = row.Cells[2].Value.ToString(); 
     wc.DownloadFileCompleted += new AsyncCompletedEventHandler(AtlasCompleted); 
     Uri fileUrl = new Uri(value1); 
     Beta = fileUrl; 
     //Console.WriteLine(FormPopup.Variables.Location1.Length); 
     if (FormPopup.Variables.Location1 != null && FormPopup.Variables.Location1.Length >= 5) 
     { 
      Console.WriteLine(FormPopup.Variables.Location1); 
      Console.WriteLine(fileUrl); 
      wc.DownloadFileAsync(fileUrl, FormPopup.Variables.Location1); 
      //MessageBox.Show(fileUrl.ToString() + "    " + FormPopup.Variables.Location1); 
     } 
     else 
     { 
      MessageBox.Show("Error: No file location specified."); 
      FormPopup form = new FormPopup(); 
      form.Show(this); 
     } 

    } 
} 

private void AtlasCompleted(object sender, AsyncCompletedEventArgs e) 
{ 
    MessageBox.Show(Beta.ToString() + "    " + FormPopup.Variables.Location1); 
} 

該文件應該下載,但它不是下載或出現在指定的位置。

如果任何人都可以提供幫助,那就太棒了,它讓我很困惑。

感謝您的答覆:d

+0

你在哪裏指定保存文件的路徑? AtlasCompleted被稱爲? –

+0

我以另一種形式指定路徑,將其保存在FormPopup.Variables.Location1下,它顯示正確的路徑,並且正在調用AtlasCompleted,但該文件沒有找到的位置。 – ZiiM

+0

我注意到一件事,該程序似乎即刻完成它。似乎沒有時間。 – ZiiM

回答

0

Web客戶端的代碼是好的,它應該下載該文件沒有任何問題。只要確保你的文件Uri是正確的(將uri粘貼到瀏覽器中並查看其正確)。在本地系統上保存文件的路徑應該是有效的(路徑中必須包含文件夾)並且普通用戶必須有權寫入。

爲了進一步測試它,先用硬編碼值:

wc.DownloadFileAsync("File Uril","File path at local system"); 

例子:

wc.DownloadFileAsync(new Uri("http://example.com/myfile.txt"), @"d:\myfile.txt"); 

仔細檢查您的烏里和位置變量,因爲沒有魔法,將防止下載除了無效的參數。

此外,添加一些錯誤記錄/異常處理,以便它告訴你發生了什麼。

private void AtlasCompleted(object sender, AsyncCompletedEventArgs e) 
{ 
if(e.Error !=null) 
    Console.WriteLine(e.Error.Message); 
    else 
    Console.WriteLine("Completed"); 

    MessageBox.Show(Beta.ToString() + "    " + FormPopup.Variables.Location1); 
} 
相關問題