Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

File Download dialog

Posted on 2011-10-28
13
Medium Priority
?
675 Views
Last Modified: 2012-05-12

Hello,

The project that I am currently working on, requires to gather data from a website.  Once the data is found, the file associated with the data is downloaded to a directory.  Simple.

Here is the problem, once the data is found, there is a download button to click to get the file.  As you see in the code that I include, the first lines of the code the button is clicked and the file is starting to download.  This is where the problem starts.  The file download dialog appears and stops the automatic process.

The rest of the code that I have included, finds the file download dialog, and then locates the save button.  Once the button's handle is located, it will proceed to click on it.  Using Spy+, I can see that the message traffic is getting to the button, but there is no responding action.  

I have also tried to move the mouse cursor to the middle of the button and send a mouse click call to force the same action.  This hasn't accomplished the task either.

Now, there was an automatic download feature in IE, but that has been removed, in WIn7.  In XP, the feature is still there, but the same action of the file download dialog is present.

The question that I have, has anyone found a solution to this problem?  Could changing the mime type of the download file to be included in the safe downloads solve this?  

Thank you
Keith

 filedownload-problem.docx
0
Comment
Question by:MartinITllc
  • 6
  • 3
  • 2
  • +1
13 Comments
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 37046042
I don't know if you can do it using IE as the browser... but can suppress the dialog in Firefox.

If they are all the same file type then in FF the first time you download you get the dialog, and you can set the option for it to not ask you for files of the same type in the future.  You will need to specify the folder where you want it to download to.  You will also want to turn off the display of the down load window in the download manager.
0
 

Author Comment

by:MartinITllc
ID: 37046300
Chrome and Firefox are identical in this option.  Twebbrowser uses the IE engine as a standard.
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
ID: 37046353
Twebbrowser uses the IE engine as a standard.


I don't follow.... What are you saying there?
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 3

Expert Comment

by:sYk0
ID: 37049143
Why don't you use something like Synapse and completely forget about using TWebBrowser..?

In your application do users have to visually see the TWebBrowser component or are you hiding it and running through the HTML code until you find what you are looking for?

The reason I recommend Synapse is because it's NOT a visual component, you can parse HTML content in the background (the component could also save you time/bandwidth as it does NOT download any images, just plain vanilla HTML).

I used Synapse for a banking application (Synapse does support SSL if needed!) that I made, saved users having to manually download files as it logged in parsed the HTML and downloaded the files automatically.
0
 

Author Comment

by:MartinITllc
ID: 37049636
Yes, I have used Synapse and also Chilkat Active X in different projects.  The interesting part of this project is that even though it is a web page, the actual page is JSP.  All that I have is a button in the web browser and no direct access to the file behind it.

The generated jsp code is just the front end to a database.  Where the presented data is in table format, and there is a button on the left side to download the tif/txt file that you requested.  Clicking on the button is the only access to get the file.
0
 
LVL 21

Expert Comment

by:developmentguru
ID: 37054982
 Even in a JSP page, the button is normally HTML (it could be an embedded applet, in which case, it might not be worth the effort to try to figure out the mechanics).  Applets are not used extensively, in my experience, due to their large footprint.

  If it is HTML, you can either see where the click posts to or you can read the javascript of the page.  With either of those methods you should be able to duplicate the process without the browser control.  Then the only step left would be to duplicate what the button does... without the button.  I don't suppose you could provide us a link to test with?

0
 

Author Comment

by:MartinITllc
ID: 37058239
Sorry but it is not the HTML button that I have the issue with.  In the HTML document format, I can navigate to the html button.  Have the application click on the button and start to download a file from a website.  The problem is not with the HTML but how IE takes care of the download.  When you start a download in IE displays the File Download dialog.

I am looking for a solution to have my application click or simulate clicking the "Save" button within the File Download Dialog.

My point about JSP is that I cannot directly call the file download by HTML GET.  the file is hidden within a JSP site that serves an Oracle Database.  Otherwise, a simple one call would get the file.
0
 
LVL 21

Expert Comment

by:developmentguru
ID: 37059066
OK, thanks for the clarification.  Are you familiar with using FindWindow, GetWindowHandle and EnumChildWindows Windows API functions?  There are many examples on EE on how to use these API function within Delphi.  Using them you should be able to 1) watch for the Save dialog to be shown 2) enumerate the child controls (to find the save button) and 3) click the button (by sending messages to simulate mouse down and mouse up).  This link seems to show the basics of finding the window by title.  

http://www.experts-exchange.com/Programming/Languages/Pascal/Delphi/Q_27187092.html?sfQueryTermInfo=1+10+30+delphi+down+enumchildwindow+mous

I like to look by the window class.  This link shows how to get window class names of active windows (this will get you the class name to search for).
http://stackoverflow.com/questions/7096542/collect-all-active-window-class-names

Using FindWindow should easily allow you to find the window by name.
http://www.swissdelphicenter.ch/torry/showcode.php?id=327

Once you have the window handle, EnumChildWindows and look for the button.

Let me know if you need more.

0
 

Author Comment

by:MartinITllc
ID: 37069449
Thank you for your help, but this is all done.  Once I have the html button clicked as so:

          WB.OleObject.document.Forms.item(5).elements.item(I).click;

Open in new window


The application starts a timer waiting for the "File Download dialog" to show.  The timer is due to the time lag between clicking of the button and the appearance of the dialog.  Here is the code to the timer:


procedure TForm5.FileDownloadTimerTimer(Sender: TObject);
Var
  WHandle, ButtonHandle: hWnd;
  ParentHandle: DWORD;
  P: Array [0 .. 256] Of Char;
  ProcessIdActif: DWORD;
Begin

  // get Thread process id from the current form
  ProcessIdActif := 0;
  GetWindowThreadProcessId(Handle, @ProcessIdActif);
  LogUpdate(datetimetostr(now) + ' [INFO] Waiting for File Download Dialog.');

  WHandle := FindWindow(Nil, Nil);
  While (WHandle <> 0) Do
    begin // While there are still windows
      P[0] := #0;
      GetWindowText(WHandle, P, 255);
      // blank caption not needed
      if P[0] <> #0 then
        begin
          // get Process Id of this window
          GetWindowThreadProcessId(WHandle, @ParentHandle);
          // get windows for the current process
          if ProcessIdActif = ParentHandle then
            begin
              if CompareText(P, 'File Download') = 0 then
                begin
                  LogUpdate(datetimetostr(now) + ' [INFO] File Download Dialog found.');
                  ButtonHandle := FindWindowEx(WHandle, 0, 'Button', '&Save');
                  if ButtonHandle > 0 then
                    begin
                      LogUpdate(datetimetostr(now) + ' [INFO] &Save Button Found....' + inttostr(ButtonHandle));
                      SendMessage(ButtonHandle, BM_CLICK, 0, 0);
                      FileDownloadTimer.Enabled := False;
                    end;
                end;
            end;
        end;
      WHandle := GetWindow(WHandle, GW_HWNDNEXT);
    End;
end;

Open in new window


If the file dialog doesn't appear in a defined amount of time, the timer is aborted and the HTML button is reclicked.  After another defined amount of clicking the HTML button an error is raised and logged.  Simple design.

Now using Spy+ and the ButtonHandle that I get from the FindWIndowEX, the handle is correct.  Watching message traffic to the button handle, I can see that there is a sending of BM_CLICK to it.  The Save button of the File Download Dialog just will not response.  Just to check my methods, I commented the BM_CLICK section out and replaced it with mouse commands.  I moved the mouse to the button's center, and passed a series of Mouse Left Button down and up commands.  The same response, or actually the lack of response was seen from the Button.

Thank you for your help

0
 
LVL 21

Expert Comment

by:developmentguru
ID: 37071617
 Have you tried sending a WM_KEYDOWN WM_KEYUP combination to the handle of the button?

  If that does not work, the next thing I would do is to try focusing the button first WM_SETFOCUS.  If the button is a default button (normal for a dialog) then I would also try sending the enter key (down and up) to the parent of the button.  That should trigger the default action.
0
 
LVL 3

Expert Comment

by:sYk0
ID: 37075282
You could also try replacing IE (TWebBrowser) with Chromium Embedded (Note: the supporting libraries are large (28MB) if size is a factor for you), see http://code.google.com/p/delphichromiumembedded/.

I have used it for a few projects that require the rendering of Web pages but should also work for your purposes.
As far as I can remember, you can handle downloads with Chromium Embedded via one of its properties (see GetDownloadHandler).

If you insist on using TWebBrowser I'm sure I can a solution for you, let me know.
0
 

Accepted Solution

by:
MartinITllc earned 0 total points
ID: 37091830
Yes, chrome would be an alternate for the problems that I see.  I just checked it out and it would take some time to rewrite the code to use chromium.  And this project doesn't have that time.

Using the sendmessage(handle, bm_click,0,0) method will not work.  The limited access of sending messages to system level/ Internet Explorer level bars any attempt.  The work around would take longer that the project requires.  The solution was a simple AutoIT script that was called when the handle of the file download dialog was presented to the user.  This point the script would take over and all problems with it.  

Almost all problems, it seems that more than one download would screw up the timing of the script.  For these cases, a simple check to see if the previous download file is completed prior to going on to the next one, is all that is required.

Thank you all for your help.
0
 

Author Closing Comment

by:MartinITllc
ID: 37119186
it was the one that worked.  All of the others, would probably work, but the project requirements would not allow it.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
This is an update to some code that someone else posted on Experts Exchange. It is an alternate approach, I think a little easier to use, & makes sure that things like the Task Bar will update.
Loops Section Overview
Screencast - Getting to Know the Pipeline
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question