Ver Mensaje Individual
  #6  
Antiguo 16-06-2008
Avatar de dec
dec dec is offline
Moderador
 
Registrado: dic 2004
Ubicación: Alcobendas, Madrid, España
Posts: 13.142
Reputación: 36
dec Tiene un aura espectaculardec Tiene un aura espectacular
Hola,

A bote pronto se me ocurre algo como lo siguiente, basado en código fuente del maestro Seoane.

Código Delphi [-]
uses
  SysUtils, Dialogs, WinInet;

function DownloadToStream(Url: string; Stream: TStream): Boolean;
var
  hNet: HINTERNET;
  hUrl: HINTERNET;
  Buffer: PChar;
  BytesRead: DWORD;
begin
  Result := FALSE;
  hNet := InternetOpen('agent', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  if (hNet <> nil) then
  begin
    hUrl := InternetOpenUrl(hNet, PChar(Url), nil, 0,
      INTERNET_FLAG_RELOAD, 0);
    if (hUrl <> nil) then
    begin
      GetMem(Buffer,100*1024);
      try
        while (InternetReadFile(hUrl, Buffer, 100*1024, BytesRead)) do
        begin
          if (BytesRead = 0) then
          begin
            Result := TRUE;
            break;
          end;
          Stream.WriteBuffer(Buffer^,BytesRead);
        end;
      finally
        FreeMem(Buffer);
      end;
      InternetCloseHandle(hUrl);
    end;
    InternetCloseHandle(hNet);
  end;
end;

function DownloadImage(actionParam, codeParam, outputDir: string): boolean;
const
  IMG_EXT = '.jpg';
  BASE_URL = 'http://www.infortisa.com/spa/item/extractimg.cgi?action=%s&code=%s';
var
  fStream: TFileStream;
  imgUrl, imgPath: string;
begin
  imgUrl := Format(BASE_URL, [actionParam, codeParam]);
  imgPath := ExtractFilePath(outputDir) + codeParam + IMG_EXT;
  fStream := TFileStream.Create(imgPath, fmCreate);
  try
    result := DownloadToStream(imgUrl, fStream);
  finally
    fStream.Free();
  end;
end;

(* Ejemplo de uso
*)
procedure TForm1.Button1Click(Sender: TObject);
begin
  if DownloadImage('large', '420031', 'C:\') then
    ShowMessage('Imagen descargada')
  else
    ShowMessage('No pudo descargarse la imagen');
end;
__________________
David Esperalta
www.decsoftutils.com
Responder Con Cita