none
How to download a file from a System.Windows.Forms.WebBrowser control without hitting the Save File dialog? RRS feed

  • Question

  • I am working on a program to automate downloading PDFs from a secure website to help the people in the office get the files faster. These are reports that need to be generated daily, and merely the clicking and waiting for each report wastes a lot of time. So, having a program login and download it for them would be a great help.

    The website uses a lot of javascript and POSTs, so GET is not an option. (I tried a few GETs myself and failed.) So i go to the login page, fill in the username and password fields, invoke click on the login button, go to the report URL (an xhtml file with parameters) which brings up an options page, and invoke the click event on the button. Going directly to the url provided by the button or the url of the report, fails with an error message about rights. It would seem i must invoke the click event and wait for the PDF to be served.

    When i go there in Chrome, the PDF loads in the same tab and i can save the file like any PDF. When i go there in VB, the browser times out on the last step. I verified it went to the options page (by saving the document and checking) so everything until there works fine. When i used the browser control myself to click the button manually, it brought up a second tab and displayed a save file dialog.

    Is there a way to download the file without the dialog?

    As for the IE version, i am using IE11, set in the registry before the control is used:

            Dim Key = "HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\FeatureControl\FEATURE_BROWSER_EMULATION"
            Dim Value = IO.Path.GetFileName(Application.ExecutablePath) ' The name of this program must be the value.
            Dim IE11 = 11001
    
            If Not My.Computer.Registry.GetValue(Key, Value, vbNull).Equals(IE11) Then My.Computer.Registry.SetValue(Key, Value, IE11, Microsoft.Win32.RegistryValueKind.DWord)

    I did some searching and found one person using a webclient, and another using SendKeys. Is there a simpler way?





    Thursday, August 22, 2019 10:48 PM

All replies

  • Some progress. https://jwcooney.com/2014/03/31/remove-internet-explorer-open-or-save-popup/ shows how to get around the dialog after downloading it once (can that be done automatically in the registry?), and it works on Windows 10. Unchecking the option as specified has the default PDF viewer open. Now to change that to download the file instead. That would likely be automatic, and thus downloaded to the downloads directory, which means i'll then need to copy it from there, based on the default report name.

    As an aside, i've been testing this in an IE window opened from VB that stuck around after the debug run stopped. New runs are giving me a credentials error, which is weird, because i have the two browser windows showing side by side, one works, one does not.


    Thursday, August 22, 2019 11:51 PM
  • Hi,

    I'm sorry that you can't save the page from the WebBrowser control without displaying the dialog box. It is recommended that you use the handle of the windows API to find the savedialog and perform the corresponding actions. For example, when the saveialog appears, you can hide the dialog box, programmatically set the save path and click the save button.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 26, 2019 2:14 AM
    Moderator
  • Thank you for the reply.

    Ideally, the system would work without the webbrowser being shown to the user, and even while the computer is otherwise in use. So, clicking the button is not the ideal solution.

    Though, i might just have to do that anyway. I'm tempted by another solution i saw, which is to create an HttpWebRequest manually and pass the POST data. I'm just a bit wary of it because of all the clinet-side javascript

    Monday, August 26, 2019 12:21 PM
  • Brian,

    Your problem exist as long as IE4.0 exist. Microsoft made the Webbrowser a part of their OS. It did look well, however, it was not. 

    Endless articles appeared how to resuse the Webbrowser. But is is really reusing IE4.0. You cannot do thing which are impossible to do with IE4.0.

    Your question is a question since IE4.0 about the webbrowser. At least I never saw a really good solution for that, likewise for most of the things from the old articles which have shown were just a fairy tail and only possible on an end user lever for which nobody was waiting. 

    Be aware, the webbrowser has been all the time an excellent easy to use tool for presentation. But don't think you can simply use it to make a new webbrowser as Edge (which will probably be replaced by a Google clone). 


    Success
    Cor


    Monday, August 26, 2019 1:29 PM
  • All i want to do is to download these PDFs. :)

    Monday, August 26, 2019 1:53 PM
  • 感谢您的答复。

    理想情况下,系统可以在没有向用户显示的Web浏览器的情况下工作,甚至在计算机以其他方式使用时也是如此因此,单击按钮不是理想的解决方案。

    虽然,我可能只需要这样做。我是另一个我看到的解决方案所诱惑,那就是手动创建一个HttpWebRequest并传递POST数据。因为所有的clinet-side javascript,我只是有点警他它

    Hi,

    Maybe you can try the method you proposed.

    Best Regards,

    Julie


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 27, 2019 8:52 AM
    Moderator