Downloading File with WebBrowser and JavaScript without dialog boxes

Answered Downloading File with WebBrowser and JavaScript without dialog boxes

  • Monday, May 21, 2012 6:58 PM
     
     

    I know this topic is all over the web, but I seem to have a combination of issues that is either not addressed or I can't figure out that they are addressed.  Here is what I am doing.  I am using Webbrowser to log into a a website and navigate to a specific page, enter some query criteria and click a "search" button.  This button produces a table of files that fit the criteria.  I then go through the table, navigating to the file names and compare against my database to see if I want to download.  This all works well.

    The issue:  there is no URL behind these file names.  It is a JavaScript.  I am able to call the file with invokemember("click") but I get the pesky dialog box or Open or Save.  Then I get the directory dialog box.

    I have tried sendkeys, trie to capture the Webbrowser_downloadFile event, tried ObjectForScripting(not sure I did that correctly), all to no avail.  I have tried to use WebClient, but since I do not have and actual URL for the file, I did not expect it to work.  I can navigate to the href attribute, but not sure how to use it.

    Here is 1 row of the table:

    <tr onclick="selectRow(this,'115011')">
    <td>115011</td>
    <td>
    <a href="javascript:selectFileDownload('115011', '20120518')" title="FILE_NAME_20120518.XML">FILE_NAME_20120518.XML</a>
    </td>
    <td>05/18/2012</td>
    <td>05/19/2012 01:47:00</td>
    <td>USAGE_DATA</td>
    <td>Ready to Download</td>
    <td>&nbsp;</td>
    </tr>

    Again, I want to download this file (FILE_NAME_20120518.XML) without the dialog boxes, or at least handling the dialog boxes.

    Thanks for your assistance in advance.

All Replies

  • Wednesday, May 23, 2012 9:31 AM
    Moderator
     
     Answered

    Hi Gregg,

    Welcome to the MSDN forum.

    I'd clarify if the Web browser in your post is a control in Win form? If so, please take a look at this reply by Sheng Jiang about supperssing the save/open dialog in WebBrowser control:http://social.msdn.microsoft.com/Forums/en-CA/csharpgeneral/thread/03f65bb4-7e32-4238-a4f1-297e4ea8ccd2

    The documentation of IDownloadManager says you can hook up your own download manager that is effective in your webbrowser control host process by implementing IServiceProvider in the control site and implement QueryService to return an IDownloadManager object when asked for the service.

    In Windows Form's Webbrowser class, a control site is created for you by default, but you can override the control site by create your own WebBrowserSiteBase class and override the WebBrowser.CreateWebBrowserSiteBase Method.

    Moreover, if you got the download URL of the file, you can download it your self. Reference link is here:http://social.msdn.microsoft.com/Forums/en/vbgeneral/thread/794861ba-e2c4-4745-bec2-fae2ca4137be

    As for sending messages to JavaScript, I'm sorry for lacking in that. Hope some members that familiar with this will jump into this thread.

    If I misunderstood anything, please feel free and let me know.

    Best regards,


    Shanks Zen
    MSDN Community Support | Feedback to us

  • Thursday, May 24, 2012 11:42 AM
     
     

    Thanks for your reply.

    I am using the Webbrowser control in a VB.NET form.

    I do not have the URL for the file - I really wish I did, but they get or build the file with the Java script shown above.

    Can you get me started with some sample code for the WebBrowserSiteBase Class and IDownloadManager?  Not that I am opposed to learning new things at all, but this seems to be at the outer edge of my capabilities.  At least it appears that way after reading the documentation.

    Thanks for your help.

  • Thursday, May 24, 2012 8:12 PM
    Moderator
     
     Answered

    Edit***

    <a href="javascript:selectFileDownload('115011', '20120518')" title="FILE_NAME_20120518.XML">FILE_NAME_20120518.XML</a>

    This is some sort of javascript function that takes the parameters seen('115011', '20120518') and produces a URL based off of that information, I would see if I could examine that natural URL produced when using the website in its intended way. If you find that 115011 and 20120518 are some kind of value that is in the url, then you can derive some sort of string builder. Your best bet would be to get a copy of that javascript function and see what its doing, but something tells me that this isn't your website...


    If you want something you've never had, you need to do something you've never done. If you believe something to be true, then one day you will be called upon to demonstrate that truth.




  • Tuesday, October 16, 2012 6:48 PM
     
     

    Haven't you guys heard, that there is a way, using "Subclassing and/or Hooking"...?

    I'm looking for examples using either subclassing or hooking, which allows you to intercept any dialog box anywhere in windows, including webbrowser control or IE's File Download dialog, look up the book "Subclassing and Hooking with Visual Basic" - it is available on google's ebook archive, searchable and readable, i am in the middle of reading it but I am trying to figure out the parameters and what to look for and how to implement this for the File Download dialog box in particular.

    And IDownloadManager implementation will not work for VB6, and i'm tired of seeing that as a solution - because it's not. Using subclassing, you can get all the details of the dialog box, and the details of the controls on the dialog box, then you can automatically change the values in the location section, and click the Save button, all WITHOUT THE DIALOG EVER BEING SHOWN TO ANY OF THE USERS, the download will begin, and the dialog will never be seen, because you are intercepting it and taking control of it instead of the default procedure that would take control of it.

    Search on google to find some examples, I've spent my last two days on this.

    You can read the whole book here (since its no longer copy-right protected):

    http://ebookbrowse.com/subclassing-and-hooking-with-visual-basic-pdf-d319133138

    Mainly for VB6 but includes chapters for VB.NET as well.

    There is a download button on the top if you prefer to download and view/read on your pdf reader or iPad/iPhone/Kindle :). Hopefully, someone figures out a solution, as i know there are solutions in .NET.