控制文件下载

发布于 2024-12-28 07:34:21 字数 252 浏览 2 评论 0原文

我正在使用 TWebBrowser 为我的程序构建一个更新程序。 OnCreate 时,TWebBrowser 导航到给定的 URL。要下载更新,用户需要单击链接。单击链接时会出现此弹出窗口:

在此处输入图像描述

所以我想知道是否可以:

  1. 绕过该弹出窗口并允许自动下载。
  2. 设置文件下载到的固定路径。

I am building an updater for my program with the use of a TWebBrowser. OnCreate the TWebBrowser navigates to the given URL. To download the update, the user is required to click a link. When the link is clicked this popup appears:

enter image description here

So I was wondering if it was possible to:

  1. Bypass that popup and allow for automatic download.
  2. Set a fixed path that the file would download to.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惯饮孤独 2025-01-04 07:34:21

我会使用 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;

I would use Indy's TIdHTTP component for that, eg:

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;
蘑菇王子 2025-01-04 07:34:21

TWebBrowser 不是您想要的,因为您没有显示活动的 HTML 内容。正如之前所说,还有许多其他选择。基本上你想要一个 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;

TWebBrowser isn't what you want since you aren't displaying active HTML content. As was said before, there are numerous other options. Basically you want a HTTP request.

Here's a very simple example using WinInet, which will need to be adapted to your needs (threading, status messages and so-on).

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文