【问题标题】:File download from server (not downloading?)从服务器下载文件(不下载?)
【发布时间】:2013-12-11 09:13:04
【问题描述】:

我目前正在开发一个需要一个按钮的客户端,该按钮将检查目录以查看其中是否存在特定文件夹,如果不存在,它将从我的网络服务器下载一个 .zip 文件,一旦完成下载,提取说.zip 文件到一个目录。

由于某种原因,应用程序返回它已完成,但它根本没有下载任何东西。我使用在网上找到的教程 (http://www.ultimateprogrammingtutorials.info/2013/06/how-to-make-downloader-in-c.html) 做了一些修改。

这是我的下载代码:

下载按钮:

private void btnDownload_Click(object sender, EventArgs e)
    {
        if (!Directory.Exists(HardCorpsPath))
        {
            //MessageBox.Show("Downloading HardCorps Mod Pack (Full)", "Downloading", MessageBoxButtons.OK, MessageBoxIcon.Information);
            zipFileName = "HardCorps";

            HCDownloadURL = String.Format("http://www.survivaloperations.net/client/hardcorps/{0}.zip", zipFileName);

            WebClient Download_Client = new WebClient();//Declaring the webclient as Download_Client
            Download_Client.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);//the event handler
            Download_Client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);// " "
            Download_Client.DownloadFileAsync(new Uri(HCDownloadURL.Trim().ToString()), Arma2OAPath);// " "

            //extract zip
            HCZipPath = Path.Combine(Arma2OAPath, @"HardCorps.zip");

            using (var zipFile = ZipFile.Read(HCZipPath))
            {
                zipFile.ExtractAll(Arma2OAPath, ExtractExistingFileAction.OverwriteSilently);
            }


        }
        else
        {
            MessageBox.Show("Directory Validated!");
            //Read users HardCorpsPath\version.txt and compare to server version.txt
            //if server version > user version, download patch.zip where "patch" == the number version of the server's version.txt (ex: 1001.zip)
            //On download complete, extract to HardCorpsPath and overwrite silently
        }
    }//close Download Button

其余的:

private void ProgressChanged(object sender, DownloadProgressChangedEventArgs e)
    {
        pbDownloader.Value = e.ProgressPercentage;//setting the progressbar value as downloadprogress
    }
    private void Completed(object sender, AsyncCompletedEventArgs e)
    {
        MessageBox.Show("Downloading Successful ", "Download_Completed", MessageBoxButtons.OK, MessageBoxIcon.Information);//just a messagebox          
        pbDownloader.Value = (0);//resetting the progressbar
    }

在应用程序尝试解压缩不存在的文件之前,我没有收到任何错误。

相当迷茫和困惑,可以用一组新鲜的眼睛来发现问题。

谢谢!

【问题讨论】:

    标签: c# download webclient


    【解决方案1】:

    您的问题很可能是您将文件路径传递给DownloadFileAsync,而您应该传递文件名。

    string fullFileName = Arma2OAPath + "test.zip";
    Download_Client.DownloadFileAsync(new Uri(HCDownloadURL.Trim().ToString()), fullFileName );
    

    另一个问题是 DownloadFileAsync 是非阻塞的。这意味着您无需等待下载即可立即开始解压缩文件。你应该把解压到Completed()。

    【讨论】:

    • 我已将解压缩移动到 Completed() 方法并将其放在消息上方(因此它在告诉用户下载完成之前提取)但是现在我得到一个异常“无法将其读取为zip 文件”使用 DotNetZip 1.9,但它仍然正确提取 zip 文件(只是在执行时抛出异常)。这是例外pastebin.com/NTrtMgR9
    猜你喜欢
    • 2021-09-28
    • 1970-01-01
    • 2013-08-30
    • 2017-07-06
    • 2019-03-26
    • 2015-04-04
    相关资源
    最近更新 更多