2016-06-21 24 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); 
} 
相关问题