Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (2)


Save a webpage with imagesFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi 3.x (or higher)
Category:
Internet / Web
Skill Level:
Scoring:
Last Update:
12/03/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet wep-page images save
Times Scored:
6
Visits:
7546
Uploader: Ken Wilcox
Company: CMS
Reference: N/A
 
Question/Problem/Abstract:
Ever wanted to duplicate the functionality of your favorite browser and save a web page with images to disk, well here is a simple example that does just that. I've created two functions, the other function just lets you pass in a progress bar to show the status of the operation.

Please note: It requires Indy to run.
Answer:



unit URLGet;

interface

uses
  Classes, SysUtils, Forms, IdHTTP, ComCtrls;

procedure UrlDownloadToFile(URL, FileName: string); overload;

procedure UrlDownloadToFile(URL, FileName: string; PB: TProgressbar); overload;

implementation

procedure GetImages(html: string; Images: TStringList);
var
  i, j: Integer;
  tag: string;
  link: string;
begin
  html := StringReplace(html, #13#10, ' ', [rfReplaceAll]);
  i := 1;
  while (i <= Length(html)) do
  begin
    // we have a begin tag
    if html[i] = '<' then
    begin
      tag := '';
      while (i <= Length(html)) and (html[i] <> '>') do
      begin
        tag := tag + html[i];
        inc(i);
      end;
      tag := tag + html[i];
      //inc(i);

      // we have the tag, see if it is an a href
      link := '';
      if pos('SRC=', UpperCase(tag)) <> 0 then
      begin
        j := 1;
        while (j <= Length(tag)) do
        begin
          if (tag[j] = '"') or (tag[j] = '''') then
          begin
            link := '';
            inc(j);
            while (j <= Length(tag)) do
            begin
              link := link + tag[j];
              inc(j);
              if j > 12 then
              begin
                if (tag[j + 1] = '"') then
                  break;
                if (tag[j+1] = '''') then
                  break;
              end;
            end;
            link := link + tag[j];
            //inc(j);
            break;
          end;
          inc(j);
        end;

        if link <> '' then
          Images.Add(link);

      end;
    end;
    inc(i);
  end;
end;

procedure UrlDownloadToFile(URL, FileName: string);
var
  s, dir, path: string;
  i: Integer;
  ms: TMemoryStream;
  imgs, sFile: TStringList;
  HTTP: TIdHTTP;
begin
  imgs := TStringList.Create;
  HTTP := TidHTTP.Create(Application);
  sFile := TStringList.Create;
  try

    s := HTTP.Get(URL);

    if s <> '' then
    begin
      if FileName <> '' then
      begin
        path := ChangeFileExt(FileName, '') + '_files';
        CreateDir(path);
        dir := ExtractFileName(ChangeFileExt(FileName, '')) + '_files\';

        GetImages(s, imgs);

        ms := TMemoryStream.Create;
        try
          for i := 0 to pred(imgs.Count) do
          begin
            ms.Clear;
            HTTP.Get(URL + imgs[i], ms);
            ms.Position := 0;
            if ms.Size <> 0 then
              ms.SaveToFile(dir + imgs[i]);

            s := StringReplace(s, imgs[i], dir + imgs[i], [rfReplaceAll]);
          end;
        finally
          FreeAndNil(ms);
        end;

        sFile.Text := s;
        sFile.SaveToFile(FileName);
      end;
    end;
  finally
    FreeAndNil(sFile);
    FreeAndNil(HTTP);
    FreeAndNil(imgs);
  end;
end;

procedure UrlDownloadToFile(URL, FileName: string; PB: TProgressbar); overload;
var
  s, dir, path: string;
  i: Integer;
  ms: TMemoryStream;
  imgs, sFile: TStringList;
  HTTP: TIdHTTP;
begin
  if Assigned(PB) then
  begin
    imgs := TStringList.Create;
    HTTP := TidHTTP.Create(Application);
    sFile := TStringList.Create;
    try

      s := HTTP.Get(URL);

      if s <> '' then
      begin
        if FileName <> '' then
        begin
          path := ChangeFileExt(FileName, '') + '_files';
          CreateDir(path);
          dir := ExtractFileName(ChangeFileExt(FileName, '')) + '_files\';

          GetImages(s, imgs);

          ms := TMemoryStream.Create;
          try
            PB.Max := pred(imgs.Count);
            for i := 0 to pred(imgs.Count) do
            begin
              ms.Clear;
              HTTP.Get(URL + imgs[i], ms);
              ms.Position := 0;
              if ms.Size <> 0 then
                ms.SaveToFile(dir + imgs[i]);

              s := StringReplace(s, imgs[i], dir + imgs[i], [rfReplaceAll]);
              PB.Position := i;
              Application.ProcessMessages;
            end;
          finally
            FreeAndNil(ms);
          end;

          sFile.Text := s;
          sFile.SaveToFile(FileName);
        end;
      end;
    finally
      FreeAndNil(sFile);
      FreeAndNil(HTTP);
      FreeAndNil(imgs);
    end;
  end
  else
    UrlDownloadToFile(URL, FileName);
end;

end.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
I got an error
    Wilson Suarez (Mar 25 2003 9:55PM)

i 've been doing somthing like that, but i have to create my url string with parameters given by an employ id this url could be for example "http:\\somplace\image\05\07\05074520.jpg" where the 05 and 07 are directories in base of the 4 first digit of the id and th file name is the employee id, mi problem is when one employee do not already have the image on that place i got en eror "http/1.1 Object not Found" and i want to circle this error, so my program do not breack

is there any help for me?
Respond

RE: I got an error
Ken Wilcox (Feb 3 2005 6:54PM)

I'm not exactly sure what it is that you are trying to do but you might want to look into the ForceDirectories function in SysUtils or FileCtrl
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
C.A. Longen
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)