procedure WB_SaveAs_MHT(WB: TWebBrowser; FileName: TFileName);
var
Msg: IMessage;
Conf: IConfiguration;
Stream: _Stream;
URL : widestring;
begin
if not Assigned(WB.Document) then Exit;
URL := WB.LocationURL;
Msg := CoMessage.Create;
Conf := CoConfiguration.Create;
try
Msg.Configuration := Conf;
Msg.CreateMHTMLBody(URL, cdoSuppressAll, '', '');
Stream := Msg.GetStream;
Stream.SaveToFile(FileName, adSaveCreateOverWrite);
finally
Msg := nil;
Conf := nil;
Stream := nil;
end;
end;
========
use CDO_TLB, ADODB_TLB;//(要加入cdosys.dll才有这两单元,文章里有方法)
procedure TForm1.WB_SaveAs_MHT(WB: TWebBrowser; FileName: TFileName);
var
Msg: IMessage;
Conf: IConfiguration;
Stream: _Stream;
URL : widestring;
s1,s2: WideString;
begin
if not Assigned(WB.Document) then Exit;
URL := WB.LocationURL;
Msg := CoMessage.Create;
Conf := CoConfiguration.Create;
try
Msg.Configuration := Conf;
s1 := '';
s2 := '';
Msg.CreateMHTMLBody(URL, cdoSuppressAll, s1, s2);
Stream := Msg.GetStream;
Stream.SaveToFile(FileName, adSaveCreateOverWrite);
finally
Msg := nil;
Conf := nil;
Stream := nil;
end;
end;
利用系统自带的接口来实现。
在Delphi里选择 Import Type Library 这一项,再选择C:\Windows\system32\cdosys.dll这个DLL,createunit可以得到这两个文件、
CDO_TLB, ADODB_TLB
导出为MHT的方法为:
function URLPageSaveAsMHT(AURL: string; AFileName: string): Boolean;
var
Msg: IMessage;
Conf: IConfiguration;
Stream: _Stream;
begin
Result := False;
try
Msg := CoMessage.Create;
Conf := CoConfiguration.Create;
try
Msg.Configuration := Conf;
Msg.MimeFormatted := True;
Msg.CreateMHTMLBody(widestring(AURL), cdoSuppressNone, '', '');
Stream := Msg.GetStream;
Stream.SaveToFile(AFileName, adSaveCreateOverWrite);
Result := True;
finally
Msg := nil;
Conf := nil;
Stream := nil;
end;
except
//
end;
end;
WebBrowser1.ExecWB OLECMDID_SAVEAS, OLECMDEXECOPT_PROMPTUSER
https://msdn.microsoft.com/zh-cn/library/ms527024(v=exchg.10).aspx
需要注意的是CdoMHTMLFlags这个参数的取值,网上不少例子传的值是cdoSuppressAll,这样的话网页里所有的图片、音频等多媒体内容都不会打包进去,浏览时如果原网站把那张图片删掉了,那么你就看不到图片了,只有传递cdoSuppressNone时才会把那些图片、音频等多媒体内容都打包进去
https://zh.wikipedia.org/wiki/MHTML
Webarchive
Skydrive
http://www.qiniu.com
upyun.com
http://open.sinastorage.com/?c=doc&a=guide
http://www.ctyun.cn/