how to implement download handler ?

5,311 views
Skip to first unread message

Piotrek Kowalski

unread,
May 30, 2014, 2:35:33 PM5/30/14
to cefs...@googlegroups.com
Hi there,

I tried to implement download handler like this :

---------------------------------------------------------------------------
internal class DownloadHandler : IDownloadHandler
        {
            private string address;

            public DownloadHandler(string address)
            {
                this.address = address;
            }

            public bool OnBeforeDownload(string suggestedName, out string downloadPath, out bool showDialog)
            {
                downloadPath = Path.GetTempPath();
                showDialog = true;
                
                WebClient client = new WebClient();
                client.DownloadFileAsync(new Uri(this.address), Environment.CurrentDirectory + "\\" + suggestedName);

                return true;
            }

            public bool ReceivedData(byte[] data)
            {
               

                return true;
            }

            public void Complete()
            {
                throw new NotImplementedException();
            }
        }
---------------------------------------------------------------------------

Everything is ok, I downloaded file, but in this file was HTML code (source code) from website ?
It is very strange and I do not know how to properly download this file, or when ReceivedData function is called ?

Could you help me to impement this ? I need this functionality.

Jørn Hansen

unread,
Jun 4, 2014, 4:12:53 PM6/4/14
to cefs...@googlegroups.com
Hi,

Yep, I think I spotted the bug :-)
When you call WebClient.DownloadAsync() with this.address you are in fact telling WebClient to download the current webpage of the CefSharp browser object.

To provide your own download handler see general example code in the CefSharp.Example project which I just gave a few pointers to in my post today in this thread: https://groups.google.com/forum/?nomobile=true#!topic/cefsharp/VYT-Ewoa6F8

I think (hope) the interface is the same for this across CefSharp 1 and 3 == the master branch (I haven't really had hands on this area myself) BUT:

- For the master branch there is some fixes/simplification coming in here: https://github.com/amaitland/CefSharp/blob/41e4b36be1afe423826962e34a27332d052bf285/CefSharp.Example/DownloadHandler.cs

- You can find an earlier thread 1-2 month back here in the group with someone I talked to who got it working with CefSharp 1

--
Best regards,
JornH

Piotrek Kowalski

unread,
Jun 5, 2014, 12:37:53 AM6/5/14
to cefs...@googlegroups.com
Dear Jorn,
 
Thanks for your answer.
But, I still do not know when function ReceivedData will be fired and download any data ?
Could you be so kind and provide me some example code with downloading data functions filled properly ?
 
Thanks,
 
Best regards

Jørn Hansen

unread,
Jun 5, 2014, 2:03:41 AM6/5/14
to cefs...@googlegroups.com
Are you on CefSharp 1 or 3?

I just had a look at the upstream CEF3 API we are wrapping and it looks like if did in fact change:
http://magpcss.org/ceforum/apidocs3/projects/(default)/CefDownloadHandler.html
- compared to -
http://magpcss.org/ceforum/apidocs/projects/(default)/CefDownloadHandler.html

So that might explain why never see ReceivedData calls if you are on CefSharp 3/the master branch! All there is is a status notification. I would expect CEF itself does the stream handling.

Does the CEF 3 API docs support what you want to do? I guess CefSharp needs a few adjustments in this area. You are more than welcome to have a look at it if you can.

JornH

Piotrek Kowalski

unread,
Jun 5, 2014, 2:32:16 AM6/5/14
to cefs...@googlegroups.com
Dear Jorn,
 
Thanks for quick response.
I am using CefSharp ver 3 called CefSharp-master.
Is it possible (or will be) to do it in this version.
 
What do you advise me ? Use CefSharp ver 1 ?
 
Please help me because I really need this functionality.
 
Best regards

Jørn Hansen

unread,
Jun 5, 2014, 3:24:03 AM6/5/14
to cefs...@googlegroups.com
Piotrek,

It's definitely something I would like to see added to master soon. I personally think it would block us not calling it a -preX version.

Looking at the CEF API it looks like what's needed to start the download is to handle the callback: http://magpcss.org/ceforum/apidocs3/projects/(default)/CefBeforeDownloadCallback.html#Continue(constCefString&,bool)

This should be very similar to PR #380 which is just waiting for one small addition of the `mode` parameter handling: https://github.com/cefsharp/CefSharp/pull/380/files

If you already have a git clone of master it should be:

git checkout -b amaitland-feature/DialogHandler master
git pull https://github.com/amaitland/CefSharp.git feature/DialogHandler

So you see it shouldn't be a lot of effort as there is something to work from - and a lot of that is the included good example code, which I already took for a test drive.

As I said you are more than welcome to give it a try. The more the merrier! If you are new to git/GitHub I can give a few pointers.

Piotrek Kowalski

unread,
Jun 5, 2014, 3:55:28 AM6/5/14
to cefs...@googlegroups.com
Dear Jorn,
 
Thanks for response.
So, until download files will be implemented in new version of cefsharp 3 can I do this by using cefshar ver 1 ?
 
Unfortunately I do not have account in github, so I need to create it.
 
Best regards

Jørn Hansen

unread,
Jun 5, 2014, 4:23:51 AM6/5/14
to cefs...@googlegroups.com
The CefSharp 1 related thread I mentioned earlier is https://groups.google.com/forum/?nomobile=true#!topic/cefsharp/bS8PhHRlSAc

Benjamin there ended up getting it working ..

Piotrek Kowalski

unread,
Jun 5, 2014, 4:44:05 AM6/5/14
to cefs...@googlegroups.com
Thanks,
 
I'll take a look on this article and I will try it.
 
but, could you give me answer for question about when download will be implemented in cefshar ver 3 ?
 
regards

Jørn Hansen

unread,
Jun 5, 2014, 5:21:27 AM6/5/14
to cefs...@googlegroups.com
You're welcome!

This is what we have on our plate for the 31.0.0 milestone: https://github.com/cefsharp/CefSharp/issues?milestone=4&page=1&state=open

But that was this morning before I knew the API for the download handler had changed :-)
I would like it to be part of 31.0.0

Issue #159 above should be complete sitting on a branch - but it's a big feature and I don't know how soon it will land.

It's a open source project so dates vary as it depends on who scratches their own itches and how much time people can/want to contribute.

Piotrek Kowalski

unread,
Jun 5, 2014, 5:32:20 AM6/5/14
to cefs...@googlegroups.com
Dear Jorn,
 
I have finally downloaded file using CefSharp version 1.25.3, but...
 
I need webview.Find function support.
 
So, CefShar ver 3 does have Find function which is also very important for me, while ver 1.25.3 does not have this function.
 
What I need ?
 
- Downloading files
- Find function
 
Best regards

Piotrek Kowalski

unread,
Jun 5, 2014, 6:15:45 AM6/5/14
to cefs...@googlegroups.com
 
When new version will be available for support downloading ? and find function (which is already supported as well).

Alex Maitland

unread,
Jun 5, 2014, 10:14:34 PM6/5/14
to cefs...@googlegroups.com
Hi Piotrek,

Technically the current master build already has support for downloading files. It is quite likely that the interface will change as we expand/alter it to fully support the latest Cef3 API.

For now you can simply implement `IDownloadHandler` as Jørn has already suggested. An example implementation which opens a "Save" dialog and saves to disk is here:
https://github.com/amaitland/CefSharp/blob/41e4b36be1afe423826962e34a27332d052bf285/CefSharp.Example/DownloadHandler.cs

If you do choose to implement the current download handler, it is likely to change in the very near future, so it may be best to wait.

Cheers,
Alex

Piotrek Kowalski

unread,
Jun 6, 2014, 2:34:35 AM6/6/14
to cefs...@googlegroups.com
Dear Alex,
 
Thanks for response.
 
Yes I know this, but I try to implement download handler in the way like was in the previous version of cefsharp 1.25.3 :
lass DownloadHandler : IDownloadHandler
            {
                private readonly string _path;
                private Stream _stream;
                public DownloadHandler(string fileName)
                {
                    _path = Path.Combine(Path.GetTempPath(), fileName);
                    _stream = File.Create(_path);
                }
                public bool ReceivedData(byte[] data)
                {
                    _stream.Write(data, 0, data.GetLength(0));
                    return true;
                }
                public void Complete()
                {
                    _stream.Dispose();
                    _stream = null;
                    Console.WriteLine("Downloaded: {0}", _path);
                }
            }
---
 
 

In the new version :

 

bool IDownloadHandler.OnBeforeDownload(string suggestedName, out string downloadPath, out bool showDialog)
            {

                _path = Path.Combine(Path.GetTempPath(), suggestedName);
                _stream = File.Create(_path);

                downloadPath = Path.GetTempPath();
                showDialog = true;
               


                return true;
            }

            bool IDownloadHandler.ReceivedData(byte[] data)
            {
                _stream.Write(data, 0, data.GetLength(0));
                return true;
            }

           

            public void Complete()
            {
                _stream.Dispose();
                _stream = null;
            }

 
Unfortunately ReceivedData function does not fire up :(
 
I do not know how should I implement this to download file.
 
I would be very appreciate if you help me with this, because all cefsharp is greate solution for me, but only one thing which I need is downloading files :)
 
Best regards

Piotrek Kowalski

unread,
Jun 6, 2014, 4:28:44 AM6/6/14
to cefs...@googlegroups.com
Unfortunately code comes from Cefsharp 3 did not work.
 
Save dialog did not fire and show any dialog... only one thing which shows is empty form window...

Piotrek Kowalski

unread,
Jun 7, 2014, 4:17:28 PM6/7/14
to cefs...@googlegroups.com
Hi,

Do you know why showdialog did not show (or show but I can only see empty form window) ?

Or another thing -> I would like to download "silently" file and open it automatically, could you tell me how should I do this ?


Reply all
Reply to author
Forward
0 new messages