我正在使用TWebBrowser为我的程序构建更新程序。 OnCreate TWebBrowser导航到给定的URL。要下载更新,用户需要单击链接。单击链接时,将显示以下弹出窗口:
所以我想知道是否有可能:
答案 0 :(得分:9)
我会使用Indy的TIdHTTP
组件,例如:
uses
..., IdHTTP;
var
Url, LocalFile: String;
Strm: TFileStream;
begin
Url := ...;
LocalFile := ...;
Strm := TFileStream.Create(LocalFile, fmCreate);
try
try
IdHTTP.Get(Url, Strm);
finally
Strm.Free;
end;
except
DeleteFile(LocalFile);
raise;
end;
end;
答案 1 :(得分:2)
由于您没有显示活动的HTML内容,因此TWebBrowser不是您想要的。如前所述,还有许多其他选择。基本上你想要一个HTTP请求。
这是一个使用WinInet的简单示例,需要根据您的需求进行调整(线程,状态消息等)。
function DownloadURL(inURL, destfile: string): boolean;
var
hOpen: HINTERNET;
hFile: HINTERNET;
myAgent: string;
savefile: file;
amount_read: integer;
// the buffer size here generally reflects maximum MTU size.
// for efficiency sake, you don't want to use much more than this.
mybuffer: array[1..1460] of byte;
begin
Result := true;
myAgent := 'Test downloader app';
// other stuff in this call has to do with proxies, no way for me to test
hOpen := InternetOpen(PChar(myAgent), 0, nil, nil, 0);
if hOpen = nil then
begin
Result := false;
exit;
end;
try
hFile := InternetOpenURL(hOpen, PChar(inURL), nil, 0,
INTERNET_FLAG_RELOAD or INTERNET_FLAG_DONT_CACHE, 0);
if hFile = nil then
begin
Result := false;
exit;
end;
try
AssignFile(savefile, destfile);
Rewrite(savefile, 1);
InternetReadFile(hFile, @myBuffer, sizeof(mybuffer), amount_read);
repeat
Blockwrite(savefile, mybuffer, amount_read);
InternetReadFile(hFile, @myBuffer, sizeof(mybuffer), amount_read);
until amount_read = 0;
CloseFile(savefile);
finally
InternetCloseHandle(hFile);
end;
finally
InternetCloseHandle(hOpen);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
// example usage.
begin
if SaveDialog1.Execute then
begin
if DownloadURL(Edit1.Text, SaveDialog1.FileName) then
ShowMessage('file downloaded.')
else
ShowMessage('Error downloading file.');
end;
end;