petete2008,
Cita:
Empezado por petete2008
Necesito poner un progressbar para ver el proceso de descarga en la llamada a la siguiente función...
|
Revisa este código:
Código Delphi
[-]
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, WinINet, ComCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
ProgressBar1: TProgressBar;
procedure Button1Click(Sender: TObject);
private
public
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function DownLoadFile( URL, FileName: String ): boolean;
var
hInternetSession, hURL: HInternet;
Buffer: array[1..1024] of Byte;
ReadBuffer : Cardinal;
F: File;
FileSize : Cardinal;
BufLen: DWORD;
Index: DWORD;
begin
try
hInternetSession := InternetOpen(PChar(ExtractFileName(Application.ExeName)), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0 );
if not Assigned(hInternetSession) then
begin
MessageDlg('Error de Apertura de Session de Internet',mtConfirmation,[mbOK],0);
Result := False;
Exit;
end;
Index := 0;
ReadBuffer := 0;
BufLen := Sizeof(FileSize);
hURL := InternetOpenURL(hInternetSession, PChar(URL), nil, 0, 0, 0 );
if not Assigned(hURL) then
begin
MessageDlg('Error de Apertura de URL',mtConfirmation,[mbOK],0);
Result := False;
Exit;
end;
HttpQueryInfo(hURL,HTTP_QUERY_CONTENT_LENGTH or HTTP_QUERY_FLAG_NUMBER, @FileSize,BufLen,Index);
Form1.ProgressBar1.Max := FileSize;
Form1.ProgressBar1.Position := 0;
try
AssignFile(F, FileName);
Rewrite(F, 1);
repeat
Application.ProcessMessages;
InternetReadFile(hURL, @Buffer, SizeOf(Buffer), ReadBuffer);
BlockWrite(F, Buffer, ReadBuffer);
Form1.ProgressBar1.Position := Form1.ProgressBar1.Position + ReadBuffer;
until ReadBuffer = 0;
CloseFile(F);
Result := True;
except
MessageDlg('Error de I/O en Archivo para Download',mtConfirmation,[mbOK],0);
Result := False;
InternetCloseHandle(hURL);
InternetCloseHandle(hInternetSession);
Exit;
end
except
MessageDlg('Error de Apertura Internet-Session para Download',mtConfirmation,[mbOK],0);
Result := False;
InternetCloseHandle(hURL);
InternetCloseHandle(hInternetSession);
Exit;
end;
InternetCloseHandle(hURL);
InternetCloseHandle(hInternetSession);
MessageDlg('El Proceso de Download fue Realizado Satisfactoriamente',mtConfirmation,[mbOK],0);
Result := True;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
Url : string;
FileName : string;
begin
Url := 'http://terawiki.clubdelphi.com/Delphi/Manuales/?download=La_Cara_Oculta_De_Delphi_4_pdf_.zip';
FileName := 'C:\La_Cara_Oculta_De_Delphi_4_pdf_.zip';
DownLoadFile(Url,FileName);
end;
end.
El código anterior
permite descargar un archivo en Internet por medio de las funciones de WinINet (Windows Internet) y
muestra el progreso de la descarga en un control
TProgressBar. Quizás esta solución se pueda adaptar a tu proyecto.
Nota: El servidor FTP o HTTP
debe estar configurado para permitir descargas directas.
Espero sea útil
Nelson.