Registriert seit: 28. Jan 2006
Ort: Görlitz / Sachsen
489 Beiträge
Delphi 2007 Professional
|
Re: Datei Download mit SaveDialog
3. Feb 2008, 01:13
mache es nun mit
Delphi-Quellcode:
function TForm2.DownloadFile(Const Source : String; Const ToLabel: TLabel = Nil; Const PrgBar: TGauge = Nil): Boolean;
var
NetHandle: HINTERNET;
UrlHandle: HINTERNET;
Buffer: Array[0..1024] Of Char;
BytesRead, Reserved: dWord;
ReadFile: String;
NumByte: Integer;
FileSize: dWord;
s : TFilestream;
begin
if saveDialog1.Execute then
begin
saveDialog1.InitialDir := ExtractFilePath(Application.ExeName);
ReadFile := '';
ToLabel.Caption := 'Connecting...';
Application.ProcessMessages;
NetHandle := InternetOpen('MyApp', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
If Assigned(NetHandle) Then Begin
UrlHandle := InternetOpenUrl(NetHandle, PChar(Source), Nil, 0, INTERNET_FLAG_RELOAD, 0);
If Assigned(UrlHandle) Then Begin
If (ToLabel <> Nil) Or (PrgBar <> Nil) Then Begin
Buffer := '';
FileSize := SizeOf(Buffer);
Reserved := 0;
If HttpQueryInfo(UrlHandle, HTTP_QUERY_CONTENT_LENGTH, @Buffer, FileSize, Reserved) Then
FileSize := StrToIntDef(Buffer, -1);
End;
If ToLabel <> Nil Then
ToLabel.Caption := '0 of 0 bytes';
If Gauge1 <> Nil Then Begin
Gauge1.MinValue := 0;
Gauge1.MaxValue := FileSize;
Gauge1.Progress := 0;
End;
FillChar(Buffer, SizeOf(Buffer), 0);
Repeat
FillChar(Buffer, SizeOf(Buffer), 0);
InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead);
For NumByte := 0 To BytesRead - 1 Do
ReadFile := Concat(ReadFile, Buffer[NumByte]);
If ToLabel <> Nil Then
ToLabel.Caption := FormatFloat('0,000', Length(ReadFile)) + ' of ' + FormatFloat('0,000', FileSize) + ' bytes';
If Gauge1 <> Nil Then
Gauge1.Progress := Gauge1.Progress + StrToInt(IntToStr(BytesRead));
if Gauge1.Progress = FileSize then exit;
Application.ProcessMessages;
Until BytesRead = 0;
InternetCloseHandle(UrlHandle);
End;
InternetCloseHandle(NetHandle);
End;
ToLabel.Caption := '';
Application.ProcessMessages;
If Length(ReadFile) > 0 Then
Begin
If FileExists(saveDialog1.FileName) Then
DeleteFile(PChar(saveDialog1.FileName));
s := TFileStream.Create(saveDialog1.FileName, fmCreate);
with s do
begin
Try
Write(s, Length(ReadFile));
Finally
Free;
End;
end;
Result := True;
End
Else
Result := False;
end;
end;
bewegt zumindest erstmal den TGauge und das label zählz schön die bytes runter aber die Datei wird nicht erstellt (Systemweit gesucht)
hat jemand eine Idee?
Marcel
|
|
Zitat
|