顯示具有 WebBrowser 標籤的文章。 顯示所有文章
顯示具有 WebBrowser 標籤的文章。 顯示所有文章

2020年3月5日 星期四

Which is the best way to load a string (HTML code) in TWebBrowser?

procedure THTMLEdit.EditText(CONST HTMLCode: string);
var
  Doc: Variant;
begin
  if NOT Assigned(wbBrowser.Document) then
    wbBrowser.Navigate('about:blank');

  Doc := wbBrowser.Document;
  Doc.Clear;
  Doc.Write(HTMLCode);
  Doc.Close;
end;


https://stackoverflow.com/questions/39773033/which-is-the-best-way-to-load-a-string-html-code-in-twebbrowser

2020年2月25日 星期二

URLDownloadToFile - some pages is not download

It seems that Win7 does some of the work for us - that is to say, I just tried the code in a virtual machine with XP and VS2005 - I got the same problem, i.e error (from the watch window) - "hRes 0x800401e4 Invalid syntax HRESULT"

Earlier in the evening I had read that you need to make sure COM is initialized for the thread. I disregarded this and plowed on - this was fine when I used Win7 and Vs2010.

Anyway, the fix was to use CoInitialize(NULL); before the call to URLDownloadToFile - after this change, hRes holds S_OK afterwards. :)

Listing as tested with VS2005 in XP
Hide   Copy Code
#include "stdafx.h"
#include <windows.h>
#include <urlmon.h>

int main()
{
CoInitialize(NULL);
HRESULT hRes;
hRes = URLDownloadToFile(NULL, L"http://www.google.com/", L"google.com.file", 0, NULL);

return 0;
}

Posted 12-Aug-12 9:03am
enhzflep


https://www.codeproject.com/Questions/439137/URLDownloadToFile-some-pages-is-not-download

DELPHI TDownLoadURL下载网络文件



unit Unit1;

interface

uses
  //引用   Vcl.ExtActns
  Vcl.ExtActns,
  System.SysUtils, System.Types, System.UITypes, System.Classes,
  System.Variants,
  FMX.Types, FMX.Graphics, FMX.Controls, FMX.Forms, FMX.Dialogs, FMX.StdCtrls,
  IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient, IdHTTP, FMX.Edit;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    ProgressBar1: TProgressBar;
    Edit1: TEdit;
    GroupBox2: TGroupBox;
    Edit3: TEdit;
    Edit4: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    procedure URL_OnDownloadProgress(Sender: TDownLoadURL;
    Progress, ProgressMax: Cardinal; StatusCode: TURLDownloadStatus;
    StatusText: String; var Cancel: Boolean);
  public
    { Public declarations }
  end;

var

  Form1: TForm1;

implementation

{$R *.fmx}

var

  DownLoadURL1:TDownLoadURL;


//url=网络文件  'http://helloroman.oicp.net:8000/test.rar';
//Filename=保存到本地文件 'D:\Administrator\Desktop\123.rar';

function DownLoadFile(url,Filename:string):boolean;
var
  DownLoadURL1:TDownLoadURL;
begin
  try
    DownLoadURL1:=TDownLoadURL.Create(Form1);
    DownLoadURL1.URL:= url;
    DownLoadURL1.Filename:= Filename;
    DownLoadURL1.OnDownloadProgress:=Form1.URL_OnDownloadProgress;
    DownLoadURL1.ExecuteTarget(nil);
    DownLoadURL1.Free;
    Result:=true;
  except
    Result:=false;
  end;
end;


procedure DownLoadThread;
begin
  Form1.label3.Text:='0 kb / 0 kb';
  if DirectoryExists(ExtractFilePath(Form1.edit4.text)) then
  begin
    if not DownLoadFile(Form1.edit3.text,Form1.edit4.text) then
      Form1.GroupBox1.Text:='下载失败'
    else
      Form1.GroupBox1.Text:='下载完毕';
  end
  else
     SHowMessage(Form1.edit4.text + '不存在!');
end;


procedure TForm1.Button1Click(Sender: TObject);
begin
  TThread.CreateAnonymousThread(DownLoadThread).Start;
end;

function BytesToStr(iBytes: Integer): String;
var
  iKb: Integer;
begin
  iKb := Round(iBytes / 1024);
  if iKb > 1000 then
    Result := Format('%.2f MB', [iKb / 1024])
  else
    Result := Format('%d KB', [iKb]);
end;

// 获取网络文件名
function GetUrlFileName(url:string):string;
var
 str:string;
begin
 url:=StringReplace(StrRScan(PChar(url),'/'), '/', '',[rfReplaceAll]);
 if Pos('=',url) > 0 then
    url:=StringReplace(StrRScan(PChar(url),'='), '=', '',[rfReplaceAll]);
 Result:=url;
end;

procedure TForm1.URL_OnDownloadProgress(Sender: TDownLoadURL;
  Progress, ProgressMax: Cardinal; StatusCode: TURLDownloadStatus;
  StatusText: String; var Cancel: Boolean);
begin
  ProgressBar1.Max := ProgressMax div 100;
  ProgressBar1.Value := Progress div 100;

  Caption := StatusText;

  case StatusCode of
    dsFindingResource:GroupBox1.Text:='查找资源...';
    dsConnecting:GroupBox1.Text:='连接中...';
    dsRedirecting:GroupBox1.Text:='';
    dsBeginDownloadData:GroupBox1.Text:='准备下载文件...';
    dsDownloadingData:GroupBox1.Text:='下载中...';
  end;


  Edit1.Text:= Format('文件名:%s',[GetUrlFileName(Edit3.Text)]);
  label3.Text := Format('%s / %s', [BytesToStr(Progress),BytesToStr(ProgressMax)]);
end;

end.



http://www.cnblogs.com/xe2011/p/3719454.html