Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

can not download files automatic #22

Closed
toohandsome opened this issue May 25, 2019 · 3 comments
Closed

can not download files automatic #22

toohandsome opened this issue May 25, 2019 · 3 comments

Comments

@toohandsome
Copy link

1.when i click a link to download a file. it will open a new tab,and do nothing.
2.when i click downloads , it will crash
2

1

@sgbhavsar
Copy link

It seems that there is breaking change in cefsharp to register asynchjsobject which requires to be updated.

@dpbiz
Copy link

dpbiz commented Aug 26, 2019

There are some changes to make:

Remove the following from app.config (remove entire lines:

<add key="torConfigurationFile" value=""/>
<add key="torControlPassword" value=""/>
<add key="torControlPort" value="9051"/>
<add key="torDefaultConfigurationFile" value=""/>
<add key="torPath" value="Tor\Tor\tor.exe"/>

================

Update the following files:

  1. SharpBrowser.csproj, see (Feature Request - Add AnyCPU Support cefsharp/CefSharp#1714)
    a. add <CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport> to the first <PropertyGroup>
  2. MainForm.cs (see Javascript Binding v2 cefsharp/CefSharp#2246)
    a. after line 391, add the following before if (url.StartsWith("sharpbrowser:")) {:

//For legacy biding we'll still have support for this CefSharpSettings.LegacyJavascriptBindingEnabled = true;

  1. MainForm.cs - Change method on line 824 from:
public string CalcDownloadPath(DownloadItem item) {

string itemName = item.SuggestedFileName != null ? item.SuggestedFileName.GetAfterLast(".") + " file" : "downloads";

			string path = null;
			if (path != null) {
				return path;
			}

			return null;
		}


to

public string CalcDownloadPath(DownloadItem item) { return item.SuggestedFileName; }

  1. DownloadHandler.cs replace the entire file with the following code:
using CefSharp;

namespace SharpBrowser {
	internal class DownloadHandler : IDownloadHandler {
		readonly MainForm myForm;

		public DownloadHandler(MainForm form) {
			myForm = form;
		}

		public void OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback) {
			if (!callback.IsDisposed) {
				using (callback) {

					myForm.UpdateDownloadItem(downloadItem);

					// ask browser what path it wants to save the file into
					string path = myForm.CalcDownloadPath(downloadItem);

					// if file should not be saved, path will be null, so skip file
					if (path == null) {

						// skip file
						callback.Continue(path, false);

					} else {

						// open the downloads tab
						myForm.OpenDownloadsTab();
						callback.Continue(path, true);
					}

				}
			}
		}

		public void OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback) {
			myForm.UpdateDownloadItem(downloadItem);
			if (downloadItem.IsInProgress && myForm.CancelRequests.Contains(downloadItem.Id)) {
				callback.Cancel();
			}
		}
	}
}
  1. HostHandler.cs, replace line 13 from:

MainForm myForm;

to

readonly MainForm myForm;

@robinrodricks
Copy link
Collaborator

@dpbiz all fixes implemented, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants