Welcome Guest Search | Active Topics | Sign In | Register

[EO.WebBrowser] Catch link to PDF in new window and download Options
IESA
Posted: Wednesday, February 23, 2022 3:42:30 AM
Rank: Newbie
Groups: Member

Joined: 12/14/2012
Posts: 6
HI

We have a user control with a WebView of EO.WebBrowser. This control does not have multiple tabs.

For this sample, we have this basic html file:

Quote:


<a href='documentation.pdf' target='blank'>Download this</a>



We want when the user click on the link that the PDF donwloads without need to open a new page.

Is this possible?

Can you send us an example?

We tried to use NewWindowHandler and ShouldForceDownloadHandler and we could not get it.

Thanks
eo_support
Posted: Wednesday, February 23, 2022 4:49:10 PM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
Hi,

You will need to follow these steps:

1. Handle NewWindow event and accept this new window. You MUST accept the new window in order for the loader to continue loading the PDF file. If you do not accept the new window, the request dies here. See here for more information on how to accept the new window:

https://www.essentialobjects.com/doc/webbrowser/advanced/new_window

This also means that you MUST create a new UI to host the new WebView. It can be a temporary window that you close/hide later;

2. Handle ShouldForceDownload event of the NEW WebView. Since it's the new WebView that will load the PDF file, you should handle ShouldForceDownload on the new WebView. You should also handle all download related event on that WebView;

3. Once the download starts on the new WebView, you can hide the window you created in step 1;

4. Once the download completes on the new WebView, you can destroy the window you created in step 1;

It's possible to create an invisible window on step 1 so that you don't have to hide it later. For example, you can create a window with size (0, 0), or create set its location to be outside of screen (with negative left/top value).

Hope this helps. Please feel free to let us know if you have any more questions.

Thanks!
Stefan Koell
Posted: Wednesday, March 9, 2022 11:05:17 AM
Rank: Advanced Member
Groups: Member

Joined: 12/23/2013
Posts: 114
I too need to implement something like that and I'm not sure how I can check if the NewWindow event is triggered by a file download. What is the best way to do that?

For now I did it like this in the NewWindow event:

if (e.TargetUrl.EndsWith(".pdf", StringComparison.InvariantCultureIgnoreCase))
chromiumWebBrowser.ShouldForceDownload = true;
chromiumWebBrowser.Navigate(e.TargetUrl);
e.Accepted = true;

the chromiumWebBrowser is the new webview instance. Unfortunately, the code above doesn't do anything. It also seems that the original web view is broken after that (no input, no redraw, etc.). I need to close the tab/window and destroy the web view...

What am I doing wrong?
eo_support
Posted: Wednesday, March 9, 2022 11:52:42 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
Hi Stefan,

I am not sure what object is your chromiumWebBrowser, so we can't verify whether your code is correct or not. If you still have problems, you can try this two step process:

Step 1: Handle NewWindow event properly

Make sure you can DISPLAY the new PDF first. This means handling NewWindow event and let the PDF file load normally. Inside your NewWindow event you need to create your host UI (for example, a new tab that loads the PDF file). You should NOT call WebView.Navigate to load the PDF file. See here for more information on how to handle NewWindow event:

https://www.essentialobjects.com/doc/webbrowser/advanced/new_window

It is extremely important that you handle NewWindow event correctly first. Nothing is going to work if you don't get this step right;

Step 2: Change the behavior from displaying the PDF file to downloading

This step involves handling ShouldForceDownload event. Inside this event handler you can check your Url and set e.ForceDownload to true. Everything on this step is on the new WebView, not on the original WebView.

Once you get these two steps working, you can review the step by step instructions in our original reply to get the rest working.

Hope this helps.

Thanks!
Stefan Koell
Posted: Friday, March 11, 2022 5:07:00 AM
Rank: Advanced Member
Groups: Member

Joined: 12/23/2013
Posts: 114
Thanks again for your help. I was able to resolve the issue using the linked code. Navigating to the url afterwards was the culprit!

Regards,
Stefan
eo_support
Posted: Tuesday, March 15, 2022 3:48:34 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
Great. Glad to hear that the issue is resolved!


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.