CEF 下载文件扩展

1. 

实现下载文件,首先要让这个类继承CefDownloadHandler

然后重写父类的OnBeforeDownload和OnDownloadUpdated两个方法

头文件中添加 #include "include/cef_download_handler.h" 

添加公有继承 : public CefDownloadHandler

添加函数声明:

void OnBeforeDownload(
		CefRefPtr<CefBrowser> browser,
		CefRefPtr<CefDownloadItem> download_item,
		const CefString& suggested_name,
		CefRefPtr<CefBeforeDownloadCallback> callback);
void OnDownloadUpdated(
		CefRefPtr<CefBrowser> browser,
		CefRefPtr<CefDownloadItem> download_item,
		CefRefPtr<CefDownloadItemCallback> callback);

重写的代码如下:

void CCefClientHandler::OnBeforeDownload(
	CefRefPtr<CefBrowser> browser,
	CefRefPtr<CefDownloadItem> download_item,
	const CefString& suggested_name,
	CefRefPtr<CefBeforeDownloadCallback> callback)
{
	callback->Continue(download_item->GetURL(), true);
}

void CCefClientHandler::OnDownloadUpdated(
	CefRefPtr<CefBrowser> browser,
	CefRefPtr<CefDownloadItem> download_item,
	CefRefPtr<CefDownloadItemCallback> callback)
{
	if (download_item->IsComplete())
	{
		//MessageBox.Show("下载成功");
		if (browser->IsPopup() && !browser->HasDocument())
		{
			//browser->GetHost()->ParentWindowWillClose();
			browser->GetHost()->CloseBrowser(true);
		}
	}
}
问题:
这些都添加上后,在onbeforedownload接口设断点,下载文件一直进不了断点。

原因是没有添加获取句柄的相应构造函数。

virtual CefRefPtr<CefDownloadHandler> GetDownloadHandler() override;  // 声明
CefRefPtr<CefDownloadHandler> CCefClientHandler::GetDownloadHandler() //实现
{
	return this;
}


版权声明:本文为liuyan20092009原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。