none
Open/Save WebBrowser Control Dialog Box RRS feed

  • Question

  • Hi,
    I'm using a webbrowser control and i need that when an user click on a link that could open an open/save dialog, it doesn't appear and i can download the file in background.
    I tried with reading http response header to catch the file type in order to download the file but there aren't useful informations.
    Any idea is appreciated.

    Thanks.
    Thursday, September 25, 2008 8:49 PM

Answers

  • Hello,

           If you want to download according to the file type, and complete the process in background, I think maybe you could try the following codes.  Here I tried to download the zip file.  

            private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)

            {

                if (e.Url.Segments[e.Url.Segments.Length - 1].EndsWith(".zip"))

                {

                    e.Cancel = true;

                    //MessageBox.Show("Cancel");

                    string filepath = null;

     

                    saveFileDialog1.FileName = e.Url.Segments[e.Url.Segments.Length - 1];

                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)

                    {

                        filepath = saveFileDialog1.FileName;

     

                        WebClient client = new WebClient();

     

                        client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);

                        client.DownloadFileAsync(e.Url, filepath);
                    }

     

                   

                }

            }

            void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)  

            {  

                MessageBox.Show("File downloaded"); 

            }

           In the WebBrowser Navigating event, we get the URL information and so we could get the file type in the last item of URL.Segments array.   Then we just download the file using WebClient, and the method DownloadFileAsync let us download the file in the background.   Here I also add a handler of DownloadFileCompleted event.  

            In WebBrowser, I think the functions will be limited.   You could modify my code to meet your own target.   Hope these could inspire you somewhat. 

         

            If you have any more questions, please just let me know.

     

            Thanks,

    Best Regards,
    Lingzhi

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. http://forums.msdn.microsoft.com/en-US/csharpide/thread/8e9ed0d7-11ff-402a-8489-9b5f05eeb706 http://forums.msdn.microsoft.com/en-US/vssetup/thread/60424309-bd78-4ca2-b618-03c4a16123b6
    Tuesday, September 30, 2008 6:01 AM
    Moderator

All replies

  • Hello,

           If you want to download according to the file type, and complete the process in background, I think maybe you could try the following codes.  Here I tried to download the zip file.  

            private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e)

            {

                if (e.Url.Segments[e.Url.Segments.Length - 1].EndsWith(".zip"))

                {

                    e.Cancel = true;

                    //MessageBox.Show("Cancel");

                    string filepath = null;

     

                    saveFileDialog1.FileName = e.Url.Segments[e.Url.Segments.Length - 1];

                    if (saveFileDialog1.ShowDialog() == DialogResult.OK)

                    {

                        filepath = saveFileDialog1.FileName;

     

                        WebClient client = new WebClient();

     

                        client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);

                        client.DownloadFileAsync(e.Url, filepath);
                    }

     

                   

                }

            }

            void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)  

            {  

                MessageBox.Show("File downloaded"); 

            }

           In the WebBrowser Navigating event, we get the URL information and so we could get the file type in the last item of URL.Segments array.   Then we just download the file using WebClient, and the method DownloadFileAsync let us download the file in the background.   Here I also add a handler of DownloadFileCompleted event.  

            In WebBrowser, I think the functions will be limited.   You could modify my code to meet your own target.   Hope these could inspire you somewhat. 

         

            If you have any more questions, please just let me know.

     

            Thanks,

    Best Regards,
    Lingzhi

     


    Please remember to mark the replies as answers if they help and unmark them if they provide no help. http://forums.msdn.microsoft.com/en-US/csharpide/thread/8e9ed0d7-11ff-402a-8489-9b5f05eeb706 http://forums.msdn.microsoft.com/en-US/vssetup/thread/60424309-bd78-4ca2-b618-03c4a16123b6
    Tuesday, September 30, 2008 6:01 AM
    Moderator
  • This work perfectly if there is direct url including downloading filename.

    But sometime some url generate file dynmically. So url don't have file name but after requesting that url some website create file dynamically and then open/save dialog comes.

    for example some link generate pdf file on the fly.

    How to handle such type of url?

    Wednesday, August 18, 2010 2:31 AM
  • Hello,

    I have same like problem.

    I have a link (this link ia not a direct file link)

    which generate dynamically url to file download.

    How I can download using web browser control in specific directory.

    jayprakash

    Sunday, November 30, 2014 9:31 AM