gpt4 book ai didi

delphi - 如何防止嵌入式浏览器提示下载文件的保存位置?

转载 作者:行者123 更新时间:2023-12-03 18:16:31 25 4
gpt4 key购买 nike

如何在以编程方式单击下载按钮后下载文件,因此不需要知道下载文件的 url。

文件下载后会出现一个提示,询问您是否要保存文件,在按"is"后,另一个提示会询问您要将文件保存到哪里。所以,首先下载文件,可能是下载到某处的缓冲区中,在初始下载之后,会出现提示。

那么,单击按钮后,如何捕获下载流并将其作为文件保存在某处,而不出现弹出提示?

(点击按钮的任何方法都可以,下面应该没问题。)

procedure TForm1.Button1Click(Sender: TObject);
var
x: integer;
ovLinks: OleVariant;
begin
WebBrowser1.Navigate('The web page');
//wait for page to down load
ovLinks := WebBrowser1.OleObject.Document.all.tags('A');
if ovLinks.Length > 0 then
begin
for x := 0 to ovLinks.Length-1 do
begin
if Pos('id of button', ovLinks.Item(x).id) > 0 then
//or if Pos('href of button', ovLinks.Item(x).href) > 0 then
begin
ovLinks.Item(x).click;
Break;
end;
end;
end;
end;

这个问题的原因是:一个文件的url总是找不到。例如:在这web site ,我无法以编程方式找到 url,但在按下导出按钮后,使用 IE,文件被下载到“Internet 临时文件”文件夹中。在 IE 的“Internet 临时文件”文件夹中,它有一列“Internet 地址”,其中显示了 url。但是在 Chrome 中不存在这样的数据。但是,在这个 web site ,我可以通过编程方式找到 url,但是当我下载文件时,按“此处”,该文件不会出现在 IE 的“Internet 临时文件”文件夹中。对于其他网站,可以在文件夹中找到 url 并通过编程方式找到它,但在其他网站上,无论哪种方式都找不到 url。

最佳答案

实现IDownloadManager与它的接口(interface) Download方法添加到您的 Web 浏览器控件,您可以简单地控制您需要的内容。 Download每当您要下载文件时都会调用该方法(仅当弹出另存为对话框时)。

1.嵌入式网络浏览器

您可以使用 Embedded Web Browser控件已经实现了这个接口(interface),它触发了 OnFileDownload,它与 TWebBrowser 中的相同命名事件不同。参见例如 this thread关于如何使用它。

2.自己动手

另一种选择是您可以自己将其实现到TWebBrowser。在下面的示例中,我使用了插入类只是为了展示原理,但将其包装为一个组件非常容易(这就是我发布 OnBeforeFileDownload 的原因)。

2.1。 OnBeforeFileDownload 事件

此插入类中 TWebBrowser 的唯一扩展是 OnBeforeFileDownload 事件,该事件在将要下载文件时触发(在弹出另存为对话框之前,而是OnFileDownload 事件,而不是在下载文档本身时)。如果您不为其编写事件处理程序,Web 浏览器控件将像以前一样运行(显示另存为对话框)。如果您编写事件处理程序并将 False 返回到其 Allowed 声明的参数,则文件保存将被取消。如果您将 True 返回到 Allowed 参数(默认情况下),将显示另存为对话框。请注意,如果您通过将 Allowed 设置为 False 来取消下载,您将需要自己下载文件(正如我在本例中使用 Indy 同步执行的那样)。为此,有 FileSource 常量参数,其中包含下载文件的 URL。这是事件参数概述:

  • Sender (TObject) - 事件发送者
  • FileSource (WideString) - 源文件 URL
  • Allowed (Boolean) - 声明的 bool 参数,决定是否允许文件下载(默认值为 True)

2.2。 IDownloadManager 实现

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
StdCtrls, OleServer, OleCtrls, Dialogs, ActiveX, MSHTML, UrlMon, SHDocVw,
IdHTTP;

const
IID_IDownloadManager: TGUID = '{988934A4-064B-11D3-BB80-00104B35E7F9}';
SID_SDownloadManager: TGUID = '{988934A4-064B-11D3-BB80-00104B35E7F9}';

type
IDownloadManager = interface(IUnknown)
['{988934A4-064B-11D3-BB80-00104B35E7F9}']
function Download(pmk: IMoniker; pbc: IBindCtx; dwBindVerb: DWORD;
grfBINDF: DWORD; pBindInfo: PBindInfo; pszHeaders: PWideChar;
pszRedir: PWideChar; uiCP: UINT): HRESULT; stdcall;
end;
TBeforeFileDownloadEvent = procedure(Sender: TObject; const FileSource: WideString;
var Allowed: Boolean) of object;
TWebBrowser = class(SHDocVw.TWebBrowser, IServiceProvider, IDownloadManager)
private
FFileSource: WideString;
FOnBeforeFileDownload: TBeforeFileDownloadEvent;
function QueryService(const rsid, iid: TGUID; out Obj): HRESULT; stdcall;
function Download(pmk: IMoniker; pbc: IBindCtx; dwBindVerb: DWORD;
grfBINDF: DWORD; pBindInfo: PBindInfo; pszHeaders: PWideChar;
pszRedir: PWideChar; uiCP: UINT): HRESULT; stdcall;
protected
procedure InvokeEvent(ADispID: TDispID; var AParams: TDispParams); override;
published
property OnBeforeFileDownload: TBeforeFileDownloadEvent read FOnBeforeFileDownload write FOnBeforeFileDownload;
end;

type
TForm1 = class(TForm)
Button1: TButton;
WebBrowser1: TWebBrowser;
FileSourceLabel: TLabel;
FileSourceEdit: TEdit;
ShowDialogCheckBox: TCheckBox;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
private
procedure BeforeFileDownload(Sender: TObject; const FileSource: WideString;
var Allowed: Boolean);
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

{ TWebBrowser }

function TWebBrowser.Download(pmk: IMoniker; pbc: IBindCtx; dwBindVerb,
grfBINDF: DWORD; pBindInfo: PBindInfo; pszHeaders, pszRedir: PWideChar;
uiCP: UINT): HRESULT;
var
Allowed: Boolean;
begin
Result := E_NOTIMPL;
if Assigned(FOnBeforeFileDownload) then
begin
Allowed := True;
if pszRedir <> '' then
FFileSource := pszRedir;
FOnBeforeFileDownload(Self, FFileSource, Allowed);
if not Allowed then
Result := S_OK;
end;
end;

procedure TWebBrowser.InvokeEvent(ADispID: TDispID; var AParams: TDispParams);
begin
inherited;
// DispID 250 is the BeforeNavigate2 dispinterface and to the FFileSource here
// is stored the URL parameter (for cases, when the IDownloadManager::Download
// won't redirect the URL and pass empty string to the pszRedir)
if ADispID = 250 then
FFileSource := OleVariant(AParams.rgvarg^[5]);
end;

function TWebBrowser.QueryService(const rsid, iid: TGUID; out Obj): HRESULT;
begin
Result := E_NOINTERFACE;
Pointer(Obj) := nil;
if Assigned(FOnBeforeFileDownload) and IsEqualCLSID(rsid, SID_SDownloadManager) and
IsEqualIID(iid, IID_IDownloadManager) then
begin
if Succeeded(QueryInterface(IID_IDownloadManager, Obj)) and
Assigned(Pointer(Obj))
then
Result := S_OK;
end;
end;

{ TForm1 }

procedure TForm1.Button1Click(Sender: TObject);
var
HTMLWindow: IHTMLWindow2;
HTMLDocument: IHTMLDocument2;
begin
WebBrowser1.Navigate('http://financials.morningstar.com/income-statement/is.html?t=AAPL&ops=clear');
while WebBrowser1.ReadyState <> READYSTATE_COMPLETE do
Application.ProcessMessages;

HTMLDocument := WebBrowser1.Document as IHTMLDocument2;
if not Assigned(HTMLDocument) then
Exit;
HTMLWindow := HTMLDocument.parentWindow;
if Assigned(HTMLWindow) then
try
HTMLWindow.execScript('SRT_stocFund.Export()', 'JavaScript');
except
on E: Exception do
ShowMessage(E.Message);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
ReportMemoryLeaksOnShutdown := True;
WebBrowser1.OnBeforeFileDownload := BeforeFileDownload;
end;

procedure TForm1.BeforeFileDownload(Sender: TObject; const FileSource: WideString;
var Allowed: Boolean);
var
IdHTTP: TIdHTTP;
FileTarget: string;
FileStream: TMemoryStream;
begin
FileSourceEdit.Text := FileSource;
Allowed := ShowDialogCheckBox.Checked;
if not Allowed then
try
IdHTTP := TIdHTTP.Create(nil);
try
FileStream := TMemoryStream.Create;
try
IdHTTP.HandleRedirects := True;
IdHTTP.Get(FileSource, FileStream);
FileTarget := IdHTTP.URL.Document;
if FileTarget = '' then
FileTarget := 'File';
FileTarget := ExtractFilePath(ParamStr(0)) + FileTarget;
FileStream.SaveToFile(FileTarget);
finally
FileStream.Free;
end;
finally
IdHTTP.Free;
end;
ShowMessage('Downloading finished! File has been saved as:' + sLineBreak +
FileTarget);
except
on E: Exception do
ShowMessage(E.Message);
end;
end;

end.

2.3。 IDownloadManager 项目

您可以将上述代码(用 Delphi 2009 编写)作为完整项目下载 from here .

关于delphi - 如何防止嵌入式浏览器提示下载文件的保存位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13377779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com