-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);
                }
            }
        }

3 Answers3

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,777
  • 6
  • 31
  • 50
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,951
  • 21
  • 26
  • 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
0

This code is by ( https://ourcodeworld.com/articles/read/1446/how-to-allow-and-manipulate-downloads-in-cefsharp ) , The auto download path in the code refers to the directory where the downloaded files will be saved automatically without showing a file save dialog to the user. In the provided code, the auto download path is set to "C:\Users\sdkca\Downloads". ( Example path )

When the OnBeforeDownload method is triggered, it creates an IBeforeDownloadCallback object named callback, which is used to continue or cancel the download. The callback.Continue() method is called to specify the destination path for the downloaded file.

In the provided code, the destination path is constructed using the Path.Combine() method, which combines the auto download path (DownloadsDirectoryPath) and the suggested file name of the download item (downloadItem.SuggestedFileName). The DownloadsDirectoryPath variable contains the path to the directory where the file will be saved. In this example, the Path.Combine() method combines the DownloadsDirectoryPath and downloadItem.SuggestedFileName to create the full path for the downloaded file. The downloadItem.SuggestedFileName is the suggested name for the file provided by the server.

You can modify the DownloadsDirectoryPath variable to set the desired directory where the files should be automatically downloaded. Keep in mind that you should have appropriate permissions to write to that directory, and ensure the directory exists before running the code to avoid any exceptions.

With this implementation, when a download is triggered, the file will be automatically saved to the specified directory without any user intervention or file save dialog. The auto-download path is defined by the DownloadsDirectoryPath variable, and the file name is based on the downloadItem.SuggestedFileName.

using CefSharp;
using System;
using System.IO;

namespace CefsharpSandbox
{
    class MyCustomDownloadHandler : IDownloadHandler
    {
        public event EventHandler<DownloadItem> OnBeforeDownloadFired;

        public event EventHandler<DownloadItem> OnDownloadUpdatedFired;

        public void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
        {
            if (downloadItem.IsValid)
            {
                Console.WriteLine("== File information ========================");
                Console.WriteLine(" File URL: {0}", downloadItem.Url);
                Console.WriteLine(" Suggested FileName: {0}", downloadItem.SuggestedFileName);
                Console.WriteLine(" MimeType: {0}", downloadItem.MimeType);
                Console.WriteLine(" Content Disposition: {0}", downloadItem.ContentDisposition);
                Console.WriteLine(" Total Size: {0}", downloadItem.TotalBytes);
                Console.WriteLine("============================================");
            }

            OnBeforeDownloadFired?.Invoke(this, downloadItem);

            if (!callback.IsDisposed)
            {
                using (callback)
                {
                    // Define the Downloads Directory Path
                    // You can use a different one, in this example we will hard-code it
                    string DownloadsDirectoryPath = "C:\\Users\\sdkca\\Downloads\\";
                    
                    callback.Continue(
                        Path.Combine(
                            DownloadsDirectoryPath, 
                            downloadItem.SuggestedFileName
                        ),
                        showDialog: false
                    );
                }
            }
        }

        /// https://cefsharp.github.io/api/51.0.0/html/T_CefSharp_DownloadItem.htm
        public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
        {
            OnDownloadUpdatedFired?.Invoke(this, downloadItem);

            if (downloadItem.IsValid)
            {
                // Show progress of the download
                if (downloadItem.IsInProgress && (downloadItem.PercentComplete != 0))
                {
                    Console.WriteLine(
                        "Current Download Speed: {0} bytes ({1}%)",
                        downloadItem.CurrentSpeed,
                        downloadItem.PercentComplete
                    );
                }

                if (downloadItem.IsComplete)
                {
                    Console.WriteLine("The download has been finished !");
                }
            }
        }
    }
}