title: How to correctly download csv without 'save as' dialog' using TWebbrowser and Delphi?
body: <p>I subscribe to a secure https web page containing a button that downloads some data as csv. I am trying to automate the download without the 'save as' dialog appearing but always seem to get an empty file downloaded. I suspect it has something to do with file type I'm using with IdHttp as most of my code works correctly. 
  Please can anyone help with my use of IdHttp or see where else I am going wrong?</p>
  
  <p>The download button on the site calls some javascript to perform the download as follows</p>
  
  <pre><code>&lt;a class="dlCSV" href="javascript:void(0);"   onclick="dl_module.DownloadCsv();return false;"&gt;Download in CSV format…&lt;/a&gt;
  </code></pre>
  
  <p>In Delphi I use a TWeb browser to log on securely and navigate to the page.</p>
  
  <p>Clicking the download button in the TwebBrowser by hand shows the 'save as' dialog and then correctly downloads the csv data, defaulting to the filename 'data.csv'.</p>
  
  <p>Automating clicking the button using execScript (below) also works, again showing the 'save as' dialog and correctly downloading the data with the same default filename. </p>
  
  <pre><code>procedure TForm1.BtnClickDownloadbuttonClick(Sender: TObject);
  var  TheDocument : IHTMLDocument2;  // current HTML document
       HTMLWindow: IHTMLWindow2; // parent window of current HTML document
  begin
  TheDocument := WebBrowser1.Document as IHTMLDocument2; // Get reference to current document
  if not Assigned(TheDocument) then
      Exit;
  
  HTMLWindow := TheDocument.parentWindow;      // Get parent window of current document
  if  Assigned(HTMLWindow) then
    try
      HTMLWindow.execScript('dl_module.DownloadCsv()', 'JavaScript'); // execute JS function to do download
    except  
    on E : Exception do
       begin
         showmessage ('Exception class name = '+E.ClassName+ slinebreak
                   +  'Exception message = '+E.Message);
        end  //on E
    end;
  end;
  </code></pre>
  
  <p>Then I added TLama's code from here <a href="https://stackoverflow.com/questions/13377779/how-do-i-keep-an-embedded-browser-from-prompting-where-to-save-a-downloaded-file">How do I keep an embedded browser from prompting where to save a downloaded file?</a> to use IDownloadManager to intercept the download and prevent the 'save as' dialog. This is where it seems to go wrong as I then get an empty file downloaded, and not with the name data.csv.</p>
  
  <p>My code for function TWebBrowser.Download, TWebBrowser.InvokeEvent, function TWebBrowser.QueryService and TForm1.FormCreate are identical to that provided by TLama in the link above.</p>
  
  <p>My procedure TForm1.Button1Click is the same except that I changed the download function being called to the one on my page by changing the line</p>
  
  <pre><code>HTMLWindow.execScript('SRT_stocFund.Export()', 'JavaScript');
  </code></pre>
  
  <p>to</p>
  
  <pre><code>HTMLWindow.execScript('dl_module.DownloadCsv()', 'JavaScript');
  </code></pre>
  
  <p>and my procedure TForm1.BeforeFileDownload is identical except that because I'm on a secure site I added the variable</p>
  
  <pre><code>var
      LHandler: TIdSSLIOHandlerSocketOpenSSL; //&lt;&lt; on a https site    
  </code></pre>
  
  <p>and after creating the Filestream I added the lines </p>
  
  <pre><code>LHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil); 
  IdHTTP.IOHandler := LHandler; 
  </code></pre>
  
  <p>The issue seems to be in procedure TForm1.BeforeFileDownload where I note that the value of FileSource is 
  <a href="https://www.the_web_site_name/Ashx/GenericCSV.ashx" rel="nofollow noreferrer">https://www.the_web_site_name/Ashx/GenericCSV.ashx</a>. </p>
  
  <p>There is a short delay while IdHTTP.Get(FileSource, FileStream); executes and then a file is created on my hard disc but called 'GenericCSV.ashx' (not data.csv) and the file is zero bytes long and completely empty.</p>
  
  <p>Any ideas why its not downloading the file called data.csv (Do I somehow have to execute GenericCSV.ashx as well? if so how?)  </p>
  
  <p>For info here is my version of procedure TForm1.BeforeFileDownload</p>
  
  <pre><code>procedure TForm1.BeforeFileDownload(Sender: TObject;  const FileSource: WideString; var Allowed: Boolean);
  var
    IdHTTP: TIdHTTP;
    FileTarget: string;
    FileStream: TMemoryStream;
    LHandler: TIdSSLIOHandlerSocketOpenSSL;  // added as its a https site
  begin
    FileSourceEdit.Text := FileSource;
    Allowed := ShowDialogCheckBox.Checked;
    if not Allowed then
    try
      IdHTTP := TIdHTTP.Create(nil);
      try
        FileStream := TMemoryStream.Create;
        LHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil); //&lt;&lt;&lt; added as its a https site
        IdHTTP.IOHandler := LHandler;    //&lt;&lt;&lt; added as its a https site
        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;
  </code></pre>