Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.

download MSWord document and open it?

Post Reply
Christoph
Posts: 25
Joined: Fri Feb 23, 2018 6:31 am

download MSWord document and open it?

Post by Christoph »

Hi,

maybe for you it's a simple question:

in the OnBeforeDownload event we have.

...
callback.Cont(TempDir + suggestedName, False);

in OnDownloadUpdated event we do:

...
if downloadItem.IsComplete then
begin
MyPath := downloadItem.FullPath;
ShellExecute(MyApp.Handle, 'open', PChar(MyPath ), '', '', SW_SHOW);
end;

At the moment we do not use the OnBeforePopup event


Now, we load a page successfully (TChromium / TCEFWindowParent). From this page it is possible to download a MSWord document. The goal is to download and open it immediatly. With our solution so far it works, but a new foregroudtab is also opened

If we block the foregroudtab in the OnBeforePopup event (Result = true), then the download is no longer working.

So the question is, how to avoid a new foregroudtab when downloading a file?

Thanks in advance,
Christoph
User avatar
salvadordf
Posts: 4016
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: download MSWord document and open it?

Post by salvadordf »

It seems like that web page is not sending the right HTTP headers to start downloading that file or perhaps it uses another trick to start a download automatically.

If you can modify the web server configuration then it should be easy to send the right HTTP headers and remove automatic download tricks.

It gets complicated if you don't have access to the web server and it depends on how is that download started.

In the worst case you could use the TChromum.OnBeforeDownload event to read "browser.Identifier" and then compare that value with all the TChromium.BrowserId values to close the tab with that TChromium component once the download has started.

Use a separate TChromum component to handle your file downloads and don't close tabs automatically for all the file downloads.
Use the TChromum.OnBeforeDownload event to call StartDownload on the TChromium in charge of the file downloads.
Christoph
Posts: 25
Joined: Fri Feb 23, 2018 6:31 am

Re: download MSWord document and open it?

Post by Christoph »

Well, we are not the owner of that web page and so we can not modify the web server configuration

Interestingly the same web page in Chrome Browser (Version 73.0.3683.103) does not have problems with the download of a MSWord document.
So, why is there a difference to CEF4?

And how is it possible to check with CEF4 if the http header correct or not?
User avatar
salvadordf
Posts: 4016
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: download MSWord document and open it?

Post by salvadordf »

Christoph wrote: Mon May 06, 2019 11:53 am Interestingly the same web page in Chrome Browser (Version 73.0.3683.103) does not have problems with the download of a MSWord document.
So, why is there a difference to CEF4?
CEF4Delphi is just a CEF wrapper. It exposes all the API calls, data types, and events to applications made with Delphi and Lazarus but it's up to the developers to decide how to add a browser to each app.
Christoph wrote: Mon May 06, 2019 11:53 am And how is it possible to check with CEF4 if the http header correct or not?
Read this page for more information about those headers :
https://stackoverflow.com/questions/386 ... -downloads

Search in Google "http header to download file" and you will get much more info about that but you should study the HTML code in the website with the MSWord document too.

Perhaps you see something unusual in the link or the web page that opens when you download that file and you can use that information to block the new tab as discussed previously and start the download with a different TChromium component.
Christoph
Posts: 25
Joined: Fri Feb 23, 2018 6:31 am

Re: download MSWord document and open it?

Post by Christoph »

Hello,

it seems that we could fix the problem with the following two lines in the OnDownloadUpdated event:

if TChromium1.BrowserId <> browser.Identifier then
browser.Host.TryCloseBrowser;

The foregroudtab is visible for a moment, but as long as we have no better solution it's ok.

Anyway, is there an example for starting a download with a different TChromium component?
User avatar
salvadordf
Posts: 4016
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: download MSWord document and open it?

Post by salvadordf »

Christoph wrote: Tue May 07, 2019 9:41 am Anyway, is there an example for starting a download with a different TChromium component?
No but you can use a TChromium component in OSR mode in the main form.

Use the SimpleOSRBrowser demo as a template and remove all the mouse, keyboard and paint related events. You don't need a TBufferPanel because you don't need to show the browser contents. You only need :
  • TChromium.OnGetViewRect
  • TChromium.OnGetScreenPoint
  • TChromium.OnGetScreenInfo
  • TChromium.OnAfterCreated
  • TChromium.OnBeforePopup
  • TChromium.OnBeforeClose
The main form will need to use TForm.OnCloseQuery to destroy the all the TChromium components correctly, including this one.

Whenever you want to download a file just call TChromium.StartDownload in the OSR component.
User avatar
salvadordf
Posts: 4016
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: download MSWord document and open it?

Post by salvadordf »

I forgot to add that this will work if all the TChromium components use the same context.

If your app uses different contexts to have independent browsers then you would need to create a new TChromium in OSR mode sharing the same context than the browser containing the download link.

If the download link uses a URL with a BLOB scheme then you will not be able to use a different TChromium component to download the file.
darianmiller
Posts: 3
Joined: Wed Dec 04, 2019 8:50 pm

Re: download MSWord document and open it?

Post by darianmiller »

salvadordf wrote: Tue May 07, 2019 10:08 am No but you can use a TChromium component in OSR mode in the main form.

Use the SimpleOSRBrowser demo as a template and remove all the mouse, keyboard and paint related events. You don't need a TBufferPanel because you don't need to show the browser contents. You only need :
  • TChromium.OnGetViewRect
  • TChromium.OnGetScreenPoint
  • TChromium.OnGetScreenInfo
  • TChromium.OnAfterCreated
  • TChromium.OnBeforePopup
  • TChromium.OnBeforeClose

Not sure if something will blow up later...but to make it even simpler, OSR without caring about the actual display seems to work without defining OnGetViewRect, OnGetScreenPoint, OnGetScreenInfo.
User avatar
salvadordf
Posts: 4016
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: download MSWord document and open it?

Post by salvadordf »

You're right. You can omit OnGetViewRect, OnGetScreenPoint, OnGetScreenInfo safely in this case.

CEF4Delphi sets the default values for OnGetViewRect in case you don't use that event. It sets the viewrect to 800x600 by default because the CEF code comments state that it must set a viewrect in all cases.

OnGetScreenPoint and OnGetScreenInfo will use the original view coordinates or the default screen_info values if Result is False, which is the default value.
darianmiller
Posts: 3
Joined: Wed Dec 04, 2019 8:50 pm

Re: download MSWord document and open it?

Post by darianmiller »

Thanks for the confirmation! It seemed to be working and I was curious if something would blow up on me later. : )
Post Reply