首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >下载文件并自动保存到文件夹

问下载文件并自动保存到文件夹
EN

Stack Overflow用户
提问于 2011-07-21 17:19:34
回答 6查看 141.8K关注 0票数 10

我正在尝试制作一个从我的网站下载文件的用户界面。该网站有压缩文件,这些文件需要下载到用户输入的目录。但是,我无法成功下载该文件,它只是从一个临时文件夹打开。

代码:

代码语言:javascript
复制
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

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2011-07-29 06:04:04

为什么不完全绕过WebClient的文件处理部分呢?也许是类似下面这样的东西:

代码语言:javascript
复制
    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");
    }
票数 15
EN

Stack Overflow用户

发布于 2011-07-28 17:03:48

我的程序做的正是你想要的,没有提示或任何东西,请参阅以下代码。

这段代码将创建所有必要的目录,如果它们不存在的话:

代码语言:javascript
复制
Directory.CreateDirectory(C:\dir\dira\dirb);  // This code will create all of these directories  

此代码将把给定的文件下载到给定的目录中(在前面的代码片段创建该文件之后:

代码语言:javascript
复制
private void install()
    {
        WebClient webClient = new WebClient();                                                          // Creates a webclient
        webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);                   // Uses the Event Handler to check whether the download is complete
        webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);  // Uses the Event Handler to check for progress made
        webClient.DownloadFileAsync(new Uri("http://www.com/newfile.zip"), @"C\newfile.zip");           // Defines the URL and destination directory for the downloaded file
    }

因此,使用这两段代码,您可以创建所有目录,然后告诉下载器(这不会提示您将文件下载到该位置。

票数 20
EN

Stack Overflow用户

发布于 2015-07-09 23:15:16

如果您不想使用"WebClient“或/和需要使用System.Windows.Forms.WebBrowser,例如,您想要首先模拟登录,您可以使用这个扩展的WebBrowser,它从Windows URLMON Lib挂接"URLDownloadToFile”方法并使用WebBrowser的上下文

信息:http://www.pinvoke.net/default.aspx/urlmon/URLDownloadToFile%20.html

代码语言:javascript
复制
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace dataCoreLib.Net.Webpage
{
        public class WebBrowserWithDownloadAbility : WebBrowser
        {
            /// <summary>
            /// The URLMON library contains this function, URLDownloadToFile, which is a way
            /// to download files without user prompts.  The ExecWB( _SAVEAS ) function always
            /// prompts the user, even if _DONTPROMPTUSER parameter is specified, for "internet
            /// security reasons".  This function gets around those reasons.
            /// </summary>
            /// <param name="callerPointer">Pointer to caller object (AX).</param>
            /// <param name="url">String of the URL.</param>
            /// <param name="filePathWithName">String of the destination filename/path.</param>
            /// <param name="reserved">[reserved].</param>
            /// <param name="callBack">A callback function to monitor progress or abort.</param>
            /// <returns>0 for okay.</returns>
            /// source: http://www.pinvoke.net/default.aspx/urlmon/URLDownloadToFile%20.html
            [DllImport("urlmon.dll", CharSet = CharSet.Auto, SetLastError = true)]
            static extern Int32 URLDownloadToFile(
                [MarshalAs(UnmanagedType.IUnknown)] object callerPointer,
                [MarshalAs(UnmanagedType.LPWStr)] string url,
                [MarshalAs(UnmanagedType.LPWStr)] string filePathWithName,
                Int32 reserved,
                IntPtr callBack);


            /// <summary>
            /// Download a file from the webpage and save it to the destination without promting the user
            /// </summary>
            /// <param name="url">the url with the file</param>
            /// <param name="destinationFullPathWithName">the absolut full path with the filename as destination</param>
            /// <returns></returns>
            public FileInfo DownloadFile(string url, string destinationFullPathWithName)
            {
                URLDownloadToFile(null, url, destinationFullPathWithName, 0, IntPtr.Zero);
                return new FileInfo(destinationFullPathWithName);
            }
        }
    }
票数 19
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6773866

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档