2016-06-13 83 views
-3

我正在使用TChromium,我需要導航到特定頁面並將此頁面的特定圖像保存到文件。TChromium:如何將特定圖像保存到文件?

我知道如何導航並提取HTML源代碼以獲取圖像的地址,但我不知道如何將圖像保存到本地文件系統。

如何使用一些TChromium方法來做到這一點?

我不想使用其他組件(如TIdHTTP)來執行此操作,因爲該站點需要登錄並且該映像依賴於活動會話。

在此先感謝!

+1

這只是一個HTTP GET命令 –

+0

@DavidHeffernan不能因爲圖片顯示,用戶應該在網站上登錄。因爲這個和其他原因我想使用TChromium。 – delphirules

+3

請編輯問題以包含這些額外的細節 –

回答

3

從CEF論壇:

「CEF目前不支持緩存資源的開採 您可以找出原先由 返回的內容重寫CefRequestHandler :: OnBeforeResourceLoad(),然後執行 請求。請求自己使用CefWebURLRequest進行檢索並保存 的內容。「

另一種方法是添加一個上下文菜單,在這裏問 - TChromium how to add "Save Picture" item in Context Menu?並在TLama取得了代碼片段:

unit Unit1; 

interface 

uses 
    Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, 
    Dialogs, StdCtrls, ExtDlgs, IdHTTP, cefvcl, ceflib; 

const 
    MENU_ID_SAVE_IMAGE_AS = Ord(MENU_ID_USER_FIRST) + 1; 

type 
    TDownloader = class(TThread) 
    private 
    FURL: string; 
    FFileName: string; 
    protected 
    procedure Execute; override; 
    public 
    constructor Create(const URL, FileName: string); reintroduce; 
    end; 

type 
    TForm1 = class(TForm) 
    Button1: TButton; 
    Chromium1: TChromium; 
    SavePictureDialog1: TSavePictureDialog; 
    procedure FormCreate(Sender: TObject); 
    procedure Chromium1BeforeContextMenu(Sender: TObject; const browser: ICefBrowser; 
     const frame: ICefFrame; const params: ICefContextMenuParams; const model: ICefMenuModel); 
    procedure Chromium1ContextMenuCommand(Sender: TObject; const browser: ICefBrowser; 
     const frame: ICefFrame; const params: ICefContextMenuParams; commandId: Integer; 
     eventFlags: TCefEventFlags; out Result: Boolean); 
    private 
    { Private declarations } 
    public 
    { Public declarations } 
    end; 

var 
    Form1: TForm1; 

implementation 

{$R *.dfm} 

{ TDownloader } 

constructor TDownloader.Create(const URL, FileName: string); 
begin 
    inherited Create(False); 
    FreeOnTerminate := True; 
    FURL := URL; 
    FFileName := FileName; 
end; 

procedure TDownloader.Execute; 
var 
    HTTPClient: TIdHTTP; 
    FileStream: TFileStream; 
begin 
    try 
    HTTPClient := TIdHTTP.Create; 
    try 
     FileStream := TFileStream.Create(FFileName, fmCreate); 
     try 
     HTTPClient.Get(FURL, FileStream); 
     finally 
     FileStream.Free; 
     end; 
    finally 
     HTTPClient.Free; 
    end; 
    except 
    // error handling ignored for this example 
    end; 
end; 

{ TForm1 } 

procedure TForm1.FormCreate(Sender: TObject); 
begin 
    Chromium1.Load('http://www.google.com/'); 
end; 

procedure TForm1.Chromium1BeforeContextMenu(Sender: TObject; const browser: ICefBrowser; 
    const frame: ICefFrame; const params: ICefContextMenuParams; const model: ICefMenuModel); 
begin 
    if (CM_TYPEFLAG_MEDIA in params.TypeFlags) and (params.MediaType = CM_MEDIATYPE_IMAGE) then 
    model.AddItem(MENU_ID_SAVE_IMAGE_AS, 'Save image as...'); 
end; 

procedure TForm1.Chromium1ContextMenuCommand(Sender: TObject; const browser: ICefBrowser; 
    const frame: ICefFrame; const params: ICefContextMenuParams; commandId: Integer; 
    eventFlags: TCefEventFlags; out Result: Boolean); 
var 
    SaveDialog: TSavePictureDialog; 
begin 
    if (commandId = MENU_ID_SAVE_IMAGE_AS) then 
    begin 
    SaveDialog := TSavePictureDialog.Create(nil); 
    try 
     // SaveDialog.FileName := <here you can extract file name from params.SourceUrl>; 
     // SaveDialog.DefaultExt := <here you can extract file ext from params.SourceUrl>; 
     if SaveDialog.Execute then 
     TDownloader.Create(params.SourceUrl, SaveDialog.FileName); 
    finally 
     SaveDialog.Free; 
    end; 
    end; 
end; 

end. 

另一種方法是從頁確定所有圖像(看看在How can I use Javascript to get a list of all picture URLs available on a site?)並通過使用CefBrowserHost.StartDownload下載圖像鏈接。

相關問題