Sin
Sin

Reputation: 1864

CefSharp OnBeforeDownload handler opening Open file window instead of save window

I am using CefSharp[49] in my windows project The download handler is used to cancel the download according to specific condition.

Below is my handler, the problem I am facing is that the window opening is the 'Open File' window instead of 'Save as' window.

public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
        {
            if (!callback.IsDisposed)
            {
                using (callback)
                {
                    if (_allowDownload)
                        callback.Continue(downloadItem.SuggestedFileName, showDialog: true);
                    else
                        downloadItem.IsCancelled = !_allowDownload;
                }
            }
        }

enter image description here

Upvotes: 1

Views: 3659

Answers (1)

Sin
Sin

Reputation: 1864

i used the below code as a workaround

  public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
    {
        if (!callback.IsDisposed)
        {
            using (callback)
            {
                if (_allowDownload)
                {
                    SaveFileDialog saveFileDialog = new SaveFileDialog();
                    saveFileDialog.FileName = downloadItem.SuggestedFileName;
                    saveFileDialog.Filter ="|*"+ System.IO.Path.GetExtension(saveFileDialog.FileName);

                    if (saveFileDialog.ShowDialog() == DialogResult.OK && saveFileDialog.FileName != "")
                    {
                        downloadItem.SuggestedFileName = saveFileDialog.FileName;                            
                    }
                    else
                        **downloadItem.IsCancelled = _allowDownload;**
                }
                else
                    **downloadItem.IsCancelled = !_allowDownload;**

                callback.Continue(downloadItem.SuggestedFileName, showDialog: false);
            }

        }
    }

    public void OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
    {

        if (downloadItem.IsComplete || downloadItem.IsCancelled)
            browser.CloseBrowser(true);
    }

Upvotes: 1

Related Questions