I'm running Delphi 10.2 Tokyo with CEF4 Chromium in it. Browsing, HTML source, everything works. But I would really like to be able to download dynamically created files.
Normally I would simply use Indy, but the files I want downloaded are triggered by scripts, they don't have URL I could point Indy towards and grab them. In Chrome, it takes couple seconds for the file to generate and download dialogue to be offered.
CEF4 does support this, according to component download page at Briskbard.
In my understanding, the Chromium dialogue is cancelled by default and not even shown unless there is specific handler created for it. Rummaging through Chromium.pas, this looks like that should help:
// ICefDownloadHandler
procedure doOnBeforeDownload(const browser: ICefBrowser; const downloadItem: ICefDownloadItem; const suggestedName: ustring; const callback: ICefBeforeDownloadCallback); virtual;
procedure doOnDownloadUpdated(const browser: ICefBrowser; const downloadItem: ICefDownloadItem; const callback: ICefDownloadItemCallback); virtual;
but I have no clue what to do with it.
The only resource for this I have found was CEF3 c++ API Docs, but that didn't help much either as there is no example.
I'd appreciate some help. Ideally, skipping the dialogue and automatically downloading the file to app folder, user interaction isn't needed.
Thanks!
EDIT: With Victoria's tips, I've added two procedures, but they are not getting triggered. Website is clearly preparing the download as there's a load animation, but nothing happens.
procedure TForm1.Chromium1BeforeDownload(const browser: ICefBrowser; const downloadItem: ICefDownloadItem; const suggestedName: ustring; const callback: ICefBeforeDownloadCallback);
begin
ShowStatusText('Download triggered!');
callback.Cont('C:\'+suggestedName, False);
end;
procedure TForm1.Chromium1DownloadUpdated(const browser: ICefBrowser; const downloadItem: ICefDownloadItem; const callback: ICefDownloadItemCallback);
begin
if downloadItem.IsInProgress then
ShowStatusText('Download in progress');
if downloadItem.IsComplete then
ShowStatusText('Download complete');
if downloadItem.IsCanceled then
ShowStatusText('Download cancelled');
end;
What am I missing here?
EDIT2: Still stuck. Would this be useful?
function TChromium.CreateClientHandler(aIsOSR : boolean) : boolean;
Victoria gave the right answer to continue the download without a "Save As" dialog.
I just wanted to add that the first parameter in the callback.cont function is the full file path for the download including the file name or leave blank to use the suggested name and the default temp directory.
If you're downloading files from a web page after you logged in, then you must use the TChromium functions.
However, if you download files from pages that don't require logging in, then you can use TChromium, Indy, the Windows API or anything you like to download those files.
EDIT : Make sure you have write privileges in the destination folder. Usually you can't write in the app folder or the root directory c:\
EDIT 2 : Download CEF4Delphi again. Now the MiniBrowser has a simple downloader. Try to download that file into the same folder and see if it works with Chrome.