CEF 文件下載


轉載:https://blog.csdn.net/liuyan20092009/article/details/53819473?locationNum=6&fps=1

轉載:https://blog.csdn.net/guniwi/article/details/83013415

轉載:https://magpcss.org/ceforum/apidocs3/(CEF3較為權威的中文文檔)

轉載:https://blog.csdn.net/yingwang9/article/details/82187544(cef 本地web資源 打包 加密)

CefClient

CefClient提供訪問Browser實例的回調接口。一個CefClient實現可以在任意數量的Browser進程中共享。以下為幾個重要的回調:

  • 比如處理Browser的生命周期,右鍵菜單,對話框,通知顯示, 拖曳事件,焦點事件,鍵盤事件等等。如果沒有對某個特定的處理接口進行實現會造成什么影響,請查看cef_client.h文件中相關說明。
  • OnProcessMessageReceived在Browser收到Render進程的消息時被調用。

1.實現CEF在html里面下載文件,首先要讓我們自己的CefClient這個類公有繼承CefDownloadHandler

    

2.添加下載事件構造函數

virtual CefRefPtr<CefDownloadHandler> GetDownloadHandler() override;  // 聲明
CefRefPtr<CefDownloadHandler> CCefClientHandler::GetDownloadHandler() //實現
{
    return this;
}

 

3.然后重寫父類的OnBeforeDownload和OnDownloadUpdated兩個方法

在.h文件中添加函數聲名

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

    virtual void OnDownloadUpdated(
        CefRefPtr<CefBrowser> browser,
        CefRefPtr<CefDownloadItem> download_item,
        CefRefPtr<CefDownloadItemCallback> callback);

在.cpp中重寫函數

void CCefBrowserEventHandler::OnBeforeDownload(
    CefRefPtr<CefBrowser> browser,
    CefRefPtr<CefDownloadItem> download_item,
    const CefString& suggested_name,
    CefRefPtr<CefBeforeDownloadCallback> callback)
{

    WCHAR sMyDocuPath[800] = { 0 };
    ::SHGetSpecialFolderPathW(0, sMyDocuPath, CSIDL_PERSONAL, 0);

    wstring sDownloadPath = sMyDocuPath;
    sDownloadPath += L"\\";
    sDownloadPath += suggested_name.ToWString();

    wstring wstrExt = suggested_name.ToWString();
    string::size_type position = wstrExt.find(L".");

    if (position == -1)//沒找到,說明文件沒有擴展名,我們補上默認文件擴展名,不然下載好的文件無法查看
    {
        sDownloadPath += L".jpg";
    }

    CefString sPath;
    sPath.FromWString(sDownloadPath);
    //Param [download_path] Set |download_path| to the full file path  for the download including the file name
    //Param [show_dialog] Set | show_dialog | to true if you do wish to show the default "Save As" dialog.

    callback->Continue(sDownloadPath, true);//第一個參數是設置文件保存全路徑包含文件名
}

void CCefBrowserEventHandler::OnDownloadUpdated(
    CefRefPtr<CefBrowser> browser,
    CefRefPtr<CefDownloadItem> download_item,
    CefRefPtr<CefDownloadItemCallback> callback)
{
    if (download_item->IsComplete())//下載完成
    {
        wstring sFilePath = download_item->GetFullPath().ToWString();//拿到下載完成文件全路徑
    }
}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM