下载文件并自动保存到文件夹 [英] Download file and automatically save it to folder

查看:150
本文介绍了下载文件并自动保存到文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的网站下载文件的UI。该网站有zip文件,这些需要下载到用户输入的目录。但是,我无法成功下载文件,它只是从临时文件夹中打开。

I'm trying to make a UI for downloading files from my site. The site have zip-files and these need to be downloaded to the directory entered by the user. However, I can't succeed to download the file, it just opens up from a temporary folder.

代码:

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
{
        e.Cancel = true;
        string filepath = null;
            filepath = textBox1.Text;
            WebClient client = new WebClient();
            client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
            client.DownloadFileAsync(e.Url, filepath);
}

完整的源代码:
http://en.paidpaste.com/LqEmiQ

推荐答案

为什么不要绕过WebClient的文件处理程序。也许类似的东西:

Why not just bypass the WebClient's file handling pieces altogether. Perhaps something similar to this:

    private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)
    {
        e.Cancel = true;
        WebClient client = new WebClient();

        client.DownloadDataCompleted += new DownloadDataCompletedEventHandler(client_DownloadDataCompleted);

        client.DownloadDataAsync(e.Url);
    }

    void client_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
    {
        string filepath = textBox1.Text;
        File.WriteAllBytes(filepath, e.Result);
        MessageBox.Show("File downloaded");
    }

这篇关于下载文件并自动保存到文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆