Page 1 of 1

How to download and save as ?

Posted: Thu Apr 08, 2021 12:13 pm
by thefunkyjoint
Hi,

Let's say i navigate to a url that requires login. I've logged in this site. In a 'regular' browser, when i click on a specific link of this site, a 'Save as' windows will open so i can choose a file name ; then the site will save a file on my computer.

Using DCEF this won't happen. I can intercept the file link using the onBeforeDownload event. But how can i effectively save this file as the file name i want ? When the onBeforeDownload event is triggered, it seems nothing happens, the file is not saved ; or if it's saved, where is it saved ?

Thanks

Re: How to download and save as ?

Posted: Thu Apr 08, 2021 4:49 pm
by salvadordf
Hi,

The easiest way to do that is to use the TChromium.OnBeforeDownload event and call the callback.cont function with a default download path in the first parameter and a TRUE in the second parameter. This will allow CEF to show the default "Save as..." dialog.

Open the MiniBrowser demo and set to TRUE the second parameter of callback.cont in TMiniBrowserFrm.Chromium1BeforeDownload to test this feature.

If you prefer to show your own "Save as..." dialog you can also save a copy of the "callback" parameter in the TChromium.OnBeforeDownload event. Then send a message to the main form to show your "Save as..." dialog in the main application thread and then call callback.cont.

If you need to cancel the download use the TChromium.OnDownloadUpdated event and call "callback.cancel".

Read these documents for more details :
https://magpcss.org/ceforum/apidocs3/projects/(default)/CefDownloadHandler.html
https://magpcss.org/ceforum/apidocs3/projects/(default)/CefBeforeDownloadCallback.html
https://magpcss.org/ceforum/apidocs3/projects/(default)/CefDownloadItemCallback.html

Re: How to download and save as ?

Posted: Thu Apr 08, 2021 7:42 pm
by thefunkyjoint
Thank you, it worked !

But somehow, the 'Save as' window brings no particular extension. Let's say i'm downloading a JPG file, it's being downloaded, but without any extension. Anything i could do about this ?

The url in question shows no extension as well, but when downloading via Chrome, it will save with the correct extension.

Thanks

Re: How to download and save as ?

Posted: Thu Apr 08, 2021 8:50 pm
by salvadordf
The download_item parameter is a ICefDownloadItem which can be used to get the mime-type of that item.
https://magpcss.org/ceforum/apidocs3/projects/(default)/CefDownloadItem.html

Then use the CefGetExtensionsForMimeType function (uCEFMiscFunctions) to get the extension and show a custom dialog.

Re: How to download and save as ?

Posted: Thu Apr 08, 2021 11:29 pm
by thefunkyjoint
salvadordf wrote: Thu Apr 08, 2021 8:50 pm The download_item parameter is a ICefDownloadItem which can be used to get the mime-type of that item.
https://magpcss.org/ceforum/apidocs3/projects/(default)/CefDownloadItem.html

Then use the CefGetExtensionsForMimeType function (uCEFMiscFunctions) to get the extension and show a custom dialog.
Thanks but i found out a simpler solution : just by passing a directory and the downloaditem.SuggestedFileName to the callback.Cont , it will already open the Save as window with the correct filename and extension ! :D

callback.Cont('c:\'+downloaditem.SuggestedFileName,true);

CEF4Delphi never gets tired to surprise me with new features ! Great work as always ;)