Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8831019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:13:18+00:00 2026-06-14T08:13:18+00:00

How to down load a file after clicking a download button programatically, and therefore

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:13:19+00:00Added an answer on June 14, 2026 at 8:13 am

    Implement the IDownloadManager interface with its Download method to your web browser control and you can simply control what you need. The Download method 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 Browser control which has this interface already implemented and which fires the OnFileDownload that is different from the same named event in TWebBrowser. See for instance this thread on how to use it.

    2. Do it yourself

    Another option is that you can implement it to TWebBrowser by 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 the OnBeforeFileDownload published).

    2.1. OnBeforeFileDownload event

    The only extension to TWebBrowser in this interposed class is the OnBeforeFileDownload event which fires when the file is going to be downloaded (before save as dialog pops up, but instead of the OnFileDownload event, 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 its Allowed declared parameter, the file saving will be cancelled. If you return True to the Allowed parameter (what is by default), the save as dialog will be shown.
    Note that if you cancel downloading by setting Allowed to False, you’ll need to download the file by yourself (as I did synchronously using Indy in this example). For this purpose there’s the FileSource constant parameter, which contains the downloaded file URL. Here is the event parameters overview:

    • Sender (TObject) – event sender
    • FileSource (WideString) – source file URL
    • Allowed (Boolean) – declared boolean parameter, which decides if the file download will be allowed or not (default value is True)

    2.2. IDownloadManager implementation

    unit Unit1;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      StdCtrls, OleServer, OleCtrls, Dialogs, ActiveX, MSHTML, UrlMon, SHDocVw,
      IdHTTP;
    
    const
      IID_IDownloadManager: TGUID = '{988934A4-064B-11D3-BB80-00104B35E7F9}';
      SID_SDownloadManager: TGUID = '{988934A4-064B-11D3-BB80-00104B35E7F9}';
    
    type
      IDownloadManager = interface(IUnknown)
        ['{988934A4-064B-11D3-BB80-00104B35E7F9}']
        function Download(pmk: IMoniker; pbc: IBindCtx; dwBindVerb: DWORD;
          grfBINDF: DWORD; pBindInfo: PBindInfo; pszHeaders: PWideChar;
          pszRedir: PWideChar; uiCP: UINT): HRESULT; stdcall;
      end;
      TBeforeFileDownloadEvent = procedure(Sender: TObject; const FileSource: WideString;
        var Allowed: Boolean) of object;
      TWebBrowser = class(SHDocVw.TWebBrowser, IServiceProvider, IDownloadManager)
      private
        FFileSource: WideString;
        FOnBeforeFileDownload: TBeforeFileDownloadEvent;
        function QueryService(const rsid, iid: TGUID; out Obj): HRESULT; stdcall;
        function Download(pmk: IMoniker; pbc: IBindCtx; dwBindVerb: DWORD;
          grfBINDF: DWORD; pBindInfo: PBindInfo; pszHeaders: PWideChar;
          pszRedir: PWideChar; uiCP: UINT): HRESULT; stdcall;
      protected
        procedure InvokeEvent(ADispID: TDispID; var AParams: TDispParams); override;
      published
        property OnBeforeFileDownload: TBeforeFileDownloadEvent read FOnBeforeFileDownload write FOnBeforeFileDownload;
      end;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        WebBrowser1: TWebBrowser;
        FileSourceLabel: TLabel;
        FileSourceEdit: TEdit;
        ShowDialogCheckBox: TCheckBox;
        procedure Button1Click(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        procedure BeforeFileDownload(Sender: TObject; const FileSource: WideString;
          var Allowed: Boolean);
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
    {$R *.dfm}
    
    { TWebBrowser }
    
    function TWebBrowser.Download(pmk: IMoniker; pbc: IBindCtx; dwBindVerb,
      grfBINDF: DWORD; pBindInfo: PBindInfo; pszHeaders, pszRedir: PWideChar;
      uiCP: UINT): HRESULT;
    var
      Allowed: Boolean;
    begin
      Result := E_NOTIMPL;
      if Assigned(FOnBeforeFileDownload) then
      begin
        Allowed := True;
        if pszRedir <> '' then
          FFileSource := pszRedir;
        FOnBeforeFileDownload(Self, FFileSource, Allowed);
        if not Allowed then
          Result := S_OK;
      end;
    end;
    
    procedure TWebBrowser.InvokeEvent(ADispID: TDispID; var AParams: TDispParams);
    begin
      inherited;
      // DispID 250 is the BeforeNavigate2 dispinterface and to the FFileSource here
      // is stored the URL parameter (for cases, when the IDownloadManager::Download
      // won't redirect the URL and pass empty string to the pszRedir)
      if ADispID = 250 then
        FFileSource := OleVariant(AParams.rgvarg^[5]);
    end;
    
    function TWebBrowser.QueryService(const rsid, iid: TGUID; out Obj): HRESULT;
    begin
      Result := E_NOINTERFACE;
      Pointer(Obj) := nil;
      if Assigned(FOnBeforeFileDownload) and IsEqualCLSID(rsid, SID_SDownloadManager) and
        IsEqualIID(iid, IID_IDownloadManager) then
      begin
        if Succeeded(QueryInterface(IID_IDownloadManager, Obj)) and
          Assigned(Pointer(Obj))
        then
          Result := S_OK;
      end;
    end;
    
    { TForm1 }
    
    procedure TForm1.Button1Click(Sender: TObject);
    var
      HTMLWindow: IHTMLWindow2;
      HTMLDocument: IHTMLDocument2;
    begin
      WebBrowser1.Navigate('http://financials.morningstar.com/income-statement/is.html?t=AAPL&ops=clear');
      while WebBrowser1.ReadyState <> READYSTATE_COMPLETE do
        Application.ProcessMessages;
    
      HTMLDocument := WebBrowser1.Document as IHTMLDocument2;
      if not Assigned(HTMLDocument) then
        Exit;
      HTMLWindow := HTMLDocument.parentWindow;
      if Assigned(HTMLWindow) then
      try
        HTMLWindow.execScript('SRT_stocFund.Export()', 'JavaScript');
      except
        on E: Exception do
          ShowMessage(E.Message);
      end;
    end;
    
    procedure TForm1.FormCreate(Sender: TObject);
    begin
      ReportMemoryLeaksOnShutdown := True;
      WebBrowser1.OnBeforeFileDownload := BeforeFileDownload;
    end;
    
    procedure TForm1.BeforeFileDownload(Sender: TObject; const FileSource: WideString;
      var Allowed: Boolean);
    var
      IdHTTP: TIdHTTP;
      FileTarget: string;
      FileStream: TMemoryStream;
    begin
      FileSourceEdit.Text := FileSource;
      Allowed := ShowDialogCheckBox.Checked;
      if not Allowed then
      try
        IdHTTP := TIdHTTP.Create(nil);
        try
          FileStream := TMemoryStream.Create;
          try
            IdHTTP.HandleRedirects := True;
            IdHTTP.Get(FileSource, FileStream);
            FileTarget := IdHTTP.URL.Document;
            if FileTarget = '' then
              FileTarget := 'File';
            FileTarget := ExtractFilePath(ParamStr(0)) + FileTarget;
            FileStream.SaveToFile(FileTarget);
          finally
            FileStream.Free;
          end;
        finally
          IdHTTP.Free;
        end;
        ShowMessage('Downloading finished! File has been saved as:' + sLineBreak +
          FileTarget);
      except
        on E: Exception do
          ShowMessage(E.Message);
      end;
    end;
    
    end.
    

    2.3. IDownloadManager project

    You can download the above code (written in Delphi 2009) as a complete project from here.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a need to rename a file after download using php cURL. Here's
I cannot download file using java if the url contains special characters. eg:-http://something.com/something/this+this+this.html http://something.com/something/this%20this%20this.html
I hope to display the download file size by reading http header. I know
i have a link <a id=DownloadLink href='controller/action' target=_blank>Download File</a> that has to open a
I need to download one file from an url that i give from an
The purpose is to download the dumped backup.sql file after running the sql dumping
I've set up the remote server so clicking on the file will download it
Problem After downloading a CSV once, the form does not send a request on
I am generating several files upon clicking on a generate button. After successful generation
How can I download a XAP file to Windows Phone 7? Can't download file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.