-2

I want to remove the SaveAs dialog from CefSharp and want the file to save directly to the specified location.Nothing seems to work out please help public void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback) { OnBeforeDownloadFired?.Invoke(this, downloadItem);

            if (!callback.IsDisposed)
            {
                using (callback)
                {
                    callback.Continue("C:/Users/Wissensquelle/Downloads/bhavansh.txt", showDialog: false);
                }
            }
        }

2 Answers2

0

I want to remove the SaveAs dialog from CefSharp and want the file to save directly to the specified location.

This isn't possible to do (out of the box), if so, this would be a huge design flaw that would have great security issues.

On another note, take a look at the DownloadHandler class, specifically the OnBeforeDownload method, you could however change it to your likings (more of a hassle):

 callback.Continue(downloadItem.SuggestedFileName, showDialog: true);

To:

 callback.Continue(SOMEPATH, showDialog: false);
Trevor
  • 7,518
  • 6
  • 28
  • 49
0

My solution is based on

https://github.com/cefsharp/CefSharp.MinimalExample

and

Getting HTML from Cefsharp Browser

I just combined these. Saving the html content of a CefSharp browser instance is done like:

 browser = new ChromiumWebBrowser("www.google.com");
 //
 // .. etc etc
 // ..
 private async getSource()
 { 
   string source = await browser.GetBrowser().MainFrame.GetSourceAsync();
   string f = @"c:\temp\my.html";
   StreamWriter wr = new StreamWriter(f, false, System.Text.Encoding.Default);
   wr.Write(source);
   wr.Close();
 }

To test it, use above event, or roll your own File menu: add a Save item. Add Click event and make it async. Now call getSource() like:

 private async void saveToolStripMenuItem_Click(object sender, EventArgs e)
 {
   await getSource();
 }
Goodies
  • 1,682
  • 19
  • 23
  • The file I am saving is a text file not HTML file. – BHAVANSH ARORA Apr 10 '20 at 01:21
  • Hi Bhavansh, to save the content as text, you give your saved file the extension .TXT in my example on line 8... if you want only text information and not HTML, you should remove the tags. Refer to https://stackoverflow.com/questions/19523913/remove-html-tags-from-string-including-nbsp-in-c-sharp/30026043#30026043 – Goodies Apr 11 '20 at 09:29