WebBrowser Control File Download

I am trying to download a file using the WebBrowser control provided with Visual Basic.  The problem with the page is that for security reasons the downloads are triggered by submitting a hidden form, so I do not have a download location for the file.  I can simulate this and trigger the file to download, but the user still sees a prompt asking them if they want to save or open the file and a prompt asking them where they want to save it.  

Is there anyway that I can automatically save the file or at least set a default download path?

Thanks for any assistance that you can give.

Who is Participating?
Richie_SimonettiIT OperationsCommented:
maybe we can catch url in beforenavigate2 or FileDownload or DownloadBegin events.
If so, maybe cancelling the operation and do it ourselves.
It is an idea (From Ark's code):
Private Declare Function URLDownloadToFile Lib "urlmon" Alias "URLDownloadToFileA" (ByVal pCaller As

Long, ByVal szURL As String, ByVal szFileName As String, ByVal dwReserved As Long, ByVal lpfnCB As Long)

As Long

Public Function SaveURL(ByVal sUrl As String, ByVal sFileName As String) As Boolean
 SaveURL = URLDownloadToFile(0&, sUrl, sFile, 0, 0)
End Function

If SaveURL("http://www.somewhere.com/file.txt", "c:\folder\temp.txt") Then MsgBox "Download complete!"
As far as I know not.  But if you now where the download comes from (ftp, http) and its location, you can add code to download it through code by using wininet.dll

Should you require more info on this, just scream and I'll provide you with some code to get you started.

BrianGAuthor Commented:
Unfortunately the actual location of the file is an unknown.  A compiled CGI script takes care of the request and I do not have direct access to the file.

Thanks for your comment.

Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

Still, you can accomplish this by using wininet.dll to handle the http requests all by yourself.

There are two things to think of

You have to open (with wininet.dll) the url with the hidden field on it and save that to a tempfile.

Then you must extract some of the file:

<FORM ACTION="someurl" METHOD="post/get"

extract someurl and post/get

<INPUT TYPE="HIDDEN" NAME="somehidden"

extract all form elements that make pairs
(a pair is a value like somehidden=somevalue)

If method is post, take the actionurl and add all the pairs to it like this actionurl?pair1name=pair1value&pair2name=pair2value...

and open that url through winint.dll

If the method is GET, let me know, because it's then a total difrent approach.

So, you open that url and then you get transfered to the file that you want to download, so save it through disk and you're finished.

Please note that I have given it in simple explanations, but it's not simple at all!

Should you want to go this way, please let me know and we'll work it out togheter.

Things to do:

Make module for wininet.dll for HTTP access
Make small parser for html files to extract what we need

BrianGAuthor Commented:
I think that you have the post and get mixed up.  Get places the parm pairs to the url and the post is behind the scenes.

Of course I am dealing with the more complicated of the two.  The method is post and it is going behind the scenes.  I have tried adding the pairs to the URL just in case it took both, but it did not work.

I have used the wininet control before, so you don't have to go into too much detail on that.  I already have something set up to parse the page too.

I really appreciate your help on this!

You're right, i got them mixed-up, but that doesn't matter as long as we understand each other :-)

Please note that if you use the wininet control, it has limitations, and I don't know if it will be suitable for what were about to do.

I'll look up an article how to simulate a post submit for a form.  I've read it somewere on MSN I bellieve.

Anyway, I'll look it up and let ya know as soon as I find it

BrianGAuthor Commented:
I can submit the form by using the document property of the WebBrowser control.  With this property you have access to all of the page elements and javascript functions.  The problem with this is that it pops up the dialog.

Or are you saying that there is a way to simulate a post with the wininet control?

BrianGAuthor Commented:
These look of interest.  I need to read them further though.

For C++:


For VB?:
It seams to be much more complicated from what I read.

The code I'm about to give is in C, so we have to get it running in VB.

What I feared of is true, we have to send a header that indicates the proper content-type.

Here's the code that I found on MSDN

The information in this article applies to:

The Microsoft Foundation Classes (MFC), included with:
Microsoft Visual C++, 32-bit Enterprise Edition, version 4.2
Microsoft Visual C++, 32-bit Professional Edition, version 4.2
Microsoft Visual C++, 32-bit Enterprise Edition, version 5.0
Microsoft Visual C++, 32-bit Professional Edition, version 5.0
Microsoft Internet Explorer (Programming) versions 4.0, 4.01


To properly simulate a Form submission using WinInet, you need to send a header that indicates the proper Content-Type. For Forms, the proper Content-Type header is:

Content-Type: application/x-www-form-urlencoded

In many cases, the server does not respond appropriately if a Content-Type is not specified. For example, the Active Server Pages component of IIS 3.0 actually checks this header specifically for 'application/x-www-form- urlencoded' before adding form variables to the "Request.Form" object. This MIME/Content-Type indicates that the data of the request is a list of URL- encoded form variables. URL-encoding means that space character (ASCII 32) is encoded as '+', special character such '!' encoded in hexadecemal form as '%21'.

Here is a snippet of code that uses the MFC WinInet classes to simulate a Form POST request:

   CString strHeaders =
      _T("Content-Type: application/x-www-form-urlencoded");
   // URL-encoded form variables -
   // name = "John Doe", userid = "hithere", other = "P&Q"
   CString strFormData = _T("name=John+Doe&userid=hithere&other=P%26Q");

   CInternetSession session;
   CHttpConnection* pConnection =
   CHttpFile* pFile =
   BOOL result = pFile->SendRequest(strHeaders,
      (LPVOID)(LPCTSTR)strFormData, strFormData.GetLength());
Without MFC, the same code translates to straight SDK calls as follows:
   static TCHAR hdrs[] =
      _T("Content-Type: application/x-www-form-urlencoded");
   static TCHAR frmdata[] =
   statuc TCHAR accept[] =
      _T("Accept: */*");

   // for clarity, error-checking has been removed
   HINTERNET hSession = InternetOpen("MyAgent",
   HINTERNET hConnect = InternetConnect(hSession, _T("ServerNameHere"),
   HINTERNET hRequest = HttpOpenRequest(hConnect, "POST",
      _T("FormActionHere"), NULL, NULL, accept, 0, 1);
   HttpSendRequest(hRequest, hdrs, strlen(hdrs), frmdata, strlen(frmdata));
   // close any valid internet-handles

Much to do huh?
It looks like you answered you're own question.  It includes a VB example too...  by using the webbrowser object itself.

I would definitly give that article a go


it has an example, and what do ya have to lose?


Let me know...
Richie_SimonettiIT OperationsCommented:
BrianGAuthor Commented:

I don't have any more time to look at this today, but I really appreciate your help and I will let you know tomorrow how it turns out.

I plan on giving you full credit fo rthis, but I want to leave it out here for a while to let people take a gander and offer their opinion.

BrianGAuthor Commented:
Neither of these articles says anything about saving a file that is triggered to be downloaded via posting a form.

Any other thoughts?

Richie_SimonettiIT OperationsCommented:
Please, could you post site link? I could do a try.
BrianGAuthor Commented:
Unfortunately I cannot give the link.  It is a secured site and I can't give you the log in and password.

What is happening is similar to many site on the web when you click a link to download a file. You click on a link and you are taken to a page where they say your download will begin shortly.  The download starts and you are prompted for the location to save the file and what not.  This is like on download.com or tucows.com.

ie. WinAmp from download.com


Think of it like this, but you have to submit a form.  I don't know if I can find a site that mimicks exactly what I am doing.

Thanks for your help.

Richie_SimonettiIT OperationsCommented:
Don't give me log and password. For test that i want to do i don't need them at all.
Just need to see how it is implemented with maximun details, if possible.
BrianGAuthor Commented:
Well the file download area is beyond the log in, so you would not even be able to see it without logging into the site.

Richie_SimonettiIT OperationsCommented:
oops!, well, then...
We could work with webbrowser object model to achieve this task, but, at least, i need to see html code from those pages.
Question(s) below appears to have been abandoned. Your options are:
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you. You must tell the participants why you wish to do this, and allow for Expert response.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question. Again, please comment to advise the other participants why you wish to do this.

For special handling needs, please post a zero point question in the link below and include the question QID/link(s) that it regards.
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process.  Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, in the event new items have been created since this listing was pulled.


To view your open questions, please click the following link(s) and keep them all current with updates.

------------>  EXPERTS:  Please leave any comments regarding this question here on closing recommendations if this item remains inactive another three days.
Thank you everyone.
Moderator @ Experts Exchange

P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.
Richie_SimonettiIT OperationsCommented:
If there is not a solution already found by Brian himself...
I am in a box since i can't see how target site works.
As always, excellent job Moon...!
BrianG logged in 3/6, after this request was posted and chose not to respond.  I have therefore finalized this.  In the event BrianG returns with additional needs related to this question, please respond.
Moondancer - EE Moderator
Richie_SimonettiIT OperationsCommented:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.