How to down load a file after clicking a download button programatically, and therefore not needing to know the url for the downloading file.
After a file has downloaded a prompt comes up and asks if you’d like to save the file, after pressing ‘yes’ another prompt asks where you’d like to save the file. So, the file is downloaded first, maybe into a buffer somewhere, after the initial download, the prompts appear.
So, once the button is clicked how do you capture the downloading stream and save it as a file somewhere, without the popup prompts appearing?
(Any method for clicking a button would be fine, the following should be fine.)
procedure TForm1.Button1Click(Sender: TObject);
var
x: integer;
ovLinks: OleVariant;
begin
WebBrowser1.Navigate('The web page');
//wait for page to down load
ovLinks := WebBrowser1.OleObject.Document.all.tags('A');
if ovLinks.Length > 0 then
begin
for x := 0 to ovLinks.Length-1 do
begin
if Pos('id of button', ovLinks.Item(x).id) > 0 then
//or if Pos('href of button', ovLinks.Item(x).href) > 0 then
begin
ovLinks.Item(x).click;
Break;
end;
end;
end;
end;
The reason for this question is: the url of a file can not always be found.
Eg: At this web site, I couldn’t find the url programatically but after pressing the export button, using IE, the file was download into the ‘Temporary Internet Files’ folder. In the IE ‘Temporary Internet Files’ folder it has a column ‘Internet adress’ which shows the url. But in Chrome no such data exists. BUT, at this web site, I can find the url programatically, but when I download the file, by pressing ‘here’, the file doesn’t appear in the IE ‘Temporary Internet Files’ folder. For other websites, the url can be found in the folder and by finding it programatically, but at other sites the url can not be found either way.
Implement the
IDownloadManagerinterface with itsDownloadmethod to your web browser control and you can simply control what you need. TheDownloadmethod is called whenever you’re going to download a file (only when the save as dialog pops up).1. Embedded Web Browser
You can use the
Embedded Web Browsercontrol which has this interface already implemented and which fires theOnFileDownloadthat is different from the same named event inTWebBrowser. See for instancethis threadon how to use it.2. Do it yourself
Another option is that you can implement it to
TWebBrowserby yourself. In the following example I’ve used interposed class just for showing the principle, but it’s very easy to wrap it as a component (that’s why I’ve made theOnBeforeFileDownloadpublished).2.1. OnBeforeFileDownload event
The only extension to
TWebBrowserin this interposed class is theOnBeforeFileDownloadevent which fires when the file is going to be downloaded (before save as dialog pops up, but instead of theOnFileDownloadevent, not when the document itself is downloaded). If you won’t write the event handler for it, the web browser control will behave as before (showing a save as dialog). If you write the event handler and return False to itsAlloweddeclared parameter, the file saving will be cancelled. If you return True to theAllowedparameter (what is by default), the save as dialog will be shown.Note that if you cancel downloading by setting
Allowedto False, you’ll need to download the file by yourself (as I did synchronously using Indy in this example). For this purpose there’s theFileSourceconstant parameter, which contains the downloaded file URL. Here is the event parameters overview:2.2. IDownloadManager implementation
2.3. IDownloadManager project
You can download the above code (written in Delphi 2009) as a complete project
from here.