How can I browse for a file (and get full path)?

2,133 views
Skip to first unread message

Daryn

unread,
Sep 19, 2013, 8:06:28 PM9/19/13
to cef...@googlegroups.com
I need the user to be able to click a button (in the web content), browse for a file on Windows, and then have the javascript code on the page be able to access the path of the selected file.
In a normal browser, I can't do that as the browser only reveals the file name but not the whole path.

I'm new to CEF and this would be my first attempt to do anything other than just show the html/js content. I'm looking through documentation but a little overwhelmed, and I would appreciate any pointers in the right direction to go to achieve that.

I see a couple of leads for me to explore:

1) In the Xilium.CefGlue documentation there CefDialogHandler.OnFileDialog which sounds like what I want — "Called to run a file chooser dialog".
  • How do I initiate this file dialog, i.e. call the callback? I thought maybe HTML5 <input type="file"> tag, but I didn't see that calling back in my first attempt
  • Can I get OnFileDIalog to do what I need, that is initiated from the web content rather than from the client app code?
2) If that doesn't work, I'm seeing that there's ways in CEF to inject my own javascript function (CefV8Value.CreateFunction) that will call back into my client app. Seems like it might be an option.
3) Is there an easier way to support the user browsing for a file? I figure that is a common enough task that it shouldn't be very complicated...

Thanks for your assistance to this CEF newbie.

Daryn

unread,
Sep 19, 2013, 8:36:30 PM9/19/13
to cef...@googlegroups.com
Ok, I was a bit premature in saying I couldn't get HTML5 file input tag to work. A bit more hacking and I now see that I can get my client code called if I:
  • Provide an implementation of CefDialogHandler
  • return that from CefClient.GetDialogHandler()
  • In my OnFileDialog impementation, do a 'custom implementation' consisting of just a call to a c# file browser (e.g. OpenFileDialog). On OK call callback.Continue(filenames), on cancel call callback.Cancel(), and either way return true from OnFileDialog to say that I took care of showing the browse window.

If I can followup with a still-CEF-newbie question:

Now I've got that value in C#, and I want to make it accessible to my javascript code. Is the best way to do that to use CefV8Value.CreateString()?

Thanks

Dmitry Azaraev

unread,
Sep 20, 2013, 4:01:09 AM9/20/13
to cef...@googlegroups.com
Hi.

Now I've got that value in C#, and I want to make it accessible to my javascript code. Is the best way to do that to use CefV8Value.CreateString()?

Easiest way is provide custom scheme handler & request to it via XHR from JS. Search around, discussed many times. Very simple sample scheme handler exist in demo project.

You still can use V8, but you must note, that V8 lives in renderer process, so you will need V8 bindings, then browser<>renderer IPC, so this way complex mainly by this.

So i recommend use XHR.



--
You received this message because you are subscribed to the Google Groups "CefGlue" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cefglue+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



--
Best regards,
   Dmitry

Daryn

unread,
Sep 20, 2013, 2:40:10 PM9/20/13
to cef...@googlegroups.com, dmitry....@gmail.com
Thanks Dmitry, I'll look into XHR

moreyummystuff

unread,
Sep 23, 2013, 8:27:19 AM9/23/13
to cef...@googlegroups.com, dmitry....@gmail.com
Another way would be to setup javascript event(s) and use ExecuteJS to trigger them passing the data as parameters. If you use jQuery for your javascript it should be even easier to do this.

Anand Sion Arasavilli

unread,
Jan 13, 2014, 6:57:08 AM1/13/14
to cef...@googlegroups.com, dmitry....@gmail.com
 
Hi Dimitry
 
I am Anand trying to build a html5 desktop app using CefGlue. I was able to integrate my Html5 app with cefglue.. but my requirement is to access windows file system (read and write)
Can you please provide me some c# sample code on how to open a filedialog using cefglue CefDialogHandler.
 
Thanks in advance for your help.
Anand

Dmitry Azaraev

unread,
Jan 13, 2014, 11:20:14 AM1/13/14
to cef...@googlegroups.com
What's you did not understand with CefDialogHandler ?
You should provide CefDialogHandler.OnFileDialog implementation, which show dialog, and call CefFileDialogCallback callback when data will be ready.

Anand Sion Arasavilli

unread,
Jan 15, 2014, 1:37:06 AM1/15/14
to cef...@googlegroups.com, dmitry....@gmail.com
Hi Dimitry
 
Can you please provide some sample code of CefDialogHandler.OnFileDialog implementation.
 
- Anand

Anand Sion Arasavilli

unread,
Jan 15, 2014, 9:02:45 AM1/15/14
to cef...@googlegroups.com, dmitry....@gmail.com
Hi
 
I am trying to write some text to a file and save it to local file system using Html5 FileSystem API. but FileWriter is not working in CEFGlue.Demo.Winforms app.
 
Please provide some insight on how to handle writing and saving a file to local system (a specific location on the computer ex. c:\log.txt).
 
Thanks very mucy
Anand

Dmitry Azaraev

unread,
Jan 16, 2014, 3:36:08 PM1/16/14
to cef...@googlegroups.com
Hi.
I'm not sure that CEF are supports FileSystem API (need to be checked), if you ask about them.
If you need perform any host-specific tasks (like access to FS) - then you need perform them at application side... by calling C# code from JS side. Last, are already discussed many times, - easiest way todo it is cerate custom scheme handler and post and process request.
Reply all
Reply to author
Forward
0 new messages