Novo

Forum kao aplikacija na telefonu

Dodajte forum na početni ekran i koristite ga kao aplikaciju, sa menijem na dnu ekrana.

  • 15 odgovora
  • 3.970 pregleda

Najnovija ↓

Download problem u delphiu 22.09.2005. u 17:53 · pre 257 meseci #1
Code:

function TForm1.DownloadFile
(const fileURL, FileName: String): boolean;
const BufferSize = 1024;
var
  hSession, hURL: HInternet;
  Buffer: array[1..BufferSize] of Byte;
  BufferLen: DWORD;
  f: File;
  sAppName: string;
begin
Result:=False;
sAppName := ExtractFileName(Application.ExeName);
hSession := InternetOpen(PChar(sAppName),
                INTERNET_OPEN_TYPE_PRECONFIG,
               nil, nil, 0);
try
  hURL := InternetOpenURL(hSession,
            PChar(fileURL),
            nil,0,0,0);
  try
   AssignFile(f, FileName);
   Rewrite(f,1);
   Gauge1.MaxValue:=BufferLen;
   repeat
    InternetReadFile(hURL, @Buffer,
                     SizeOf(Buffer), BufferLen);
    BlockWrite(f, Buffer, BufferLen);
    Gauge1.Progress:=Gauge1.Progress+1;
   until BufferLen = 0;
   CloseFile(f);
   Result:=True;
  finally
   InternetCloseHandle(hURL)
  end
finally
  InternetCloseHandle(hSession)
end
end;

naisao sam na ovaj cod kada sam trazio neshto sto ce mi omoguciti download nekog filea sa interneta a da pritom mogu prikaziti progress tog downloadovanja u nekom progressbaru. problem je u tome sto se pri startovanju funkcije program zaledi do zavrsetka downloada. Da li neko zna u cemu je problem?
Re: Download problem u delphiu 22.09.2005. u 23:47 · pre 257 meseci
izmenjeno 1 put, poslednji put reiser 22.09.2005. u 23:47
  1. reiser22.09.2005. u 23:47zapis iz teksta poruke
#3
Najbolje ispod Gauge1.Progress:=Gauge1.Progress+1; linije
Re: Download problem u delphiu 23.09.2005. u 00:45 · pre 257 meseci #4
noup idalje imam problema, posto se zavrshi download applikacija pocinje da koci i jos neke gluposti. da li mozda neko zna neku drugu funkciju koja ima progress mogucnost?
Re: Download problem u delphiu 23.09.2005. u 09:17 · pre 256 meseci #5
Instaliraj Jedi-VCL komponente... tu imas sve i svasta, a izmedju ostalog i komponente za download.
Potpis
Play Store
Re: Download problem u delphiu 24.09.2005. u 00:19 · pre 256 meseci
izmenjeno 1 put, poslednji put krza 24.09.2005. u 00:35
  1. krza24.09.2005. u 00:35zapis iz teksta poruke
#6
pronasao sam download funkciju koja radi:
Code:

function DownloadFile(const Url: string; destFName: string): string;
var
  NetHandle: HINTERNET;
  UrlHandle: HINTERNET;
  Buffer: array[0..1024] of Char;
  BytesRead: dWord;
  f: File;
  i: longint;
begin
  Result := '';
  NetHandle := InternetOpen('Delphi ', INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
  if Assigned(NetHandle) then  begin
    UrlHandle := InternetOpenUrl(NetHandle, PChar(Url), nil, 0, INTERNET_FLAG_RELOAD, 0);
    if Assigned(UrlHandle) then begin
      FillChar(Buffer, SizeOf(Buffer), 0);
      AssignFile(f, destFName);
      Rewrite(f,1);
      i:=0;
      form3.Gauge2.Maxvalue:=bytesread;
      repeat
        Result := Result + Buffer;
        FillChar(Buffer, SizeOf(Buffer), 0);
        InternetReadFile(UrlHandle, @Buffer, SizeOf(Buffer), BytesRead);
        i:=myFileSize(destFName);
        form3.Gauge2.Progress:=i;
        BlockWrite(f, Buffer, BytesRead);
        Application.ProcessMessages;
      until BytesRead = 0;
      CloseFile(f);
      InternetCloseHandle(UrlHandle);
    end;
  end;
end;

sada kao sto vidite form3.Gauge2.Maxvalue:=bytesread; treba da se zameni sa velicinom filea na serveru odakle se skida. ali kako?
Re: Download problem u delphiu 24.09.2005. u 08:48 · pre 256 meseci #8
Code:

function DownloadFile(strUrlFile, strDestFile: string;
comLabel: TLabel = Nil;
comProgressBar: TProgressBar = Nil
): boolean;
var
  handleNet: HINTERNET;
  handleUrl: HINTERNET;
  arrchBuffer: Array[0..1024] Of Char;
  dwFileSize, dwReserved, dwBytesRead: dWord;
  intNumByte: Integer;
  strReadFile: String;
begin
  Result := false;
  try
    handleNet := InternetOpen(PChar(Application.Title), INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
    if Assigned(handleNet) then
      begin
        handleUrl := InternetOpenUrl(handleNet, PChar(strUrlFile), nil, 0, INTERNET_FLAG_RELOAD, 0);
        if Assigned(handleUrl) then
          begin

           // koliko je velik fajl
           arrchBuffer := '';
           dwFileSize := SizeOf(arrchBuffer);
           dwReserved := 0;
           If HttpQueryInfo(handleUrl, HTTP_QUERY_CONTENT_LENGTH, @arrchBuffer, dwFileSize, dwReserved) Then
              dwFileSize := StrToIntDef(arrchBuffer, -1);

           if comLabel <> nil Then
             comLabel.Caption := '0 od 0 bytes';
           if comProgressBar <> nil then
             begin
               comProgressBar.Min:=0;
               comProgressBar.Max:=dwFileSize;
               comProgressBar.Position:=0;
             end;

           FillChar(arrchBuffer, SizeOf(arrchBuffer), 0);
             repeat
               FillChar(arrchBuffer, SizeOf(arrchBuffer), 0);
               InternetReadFile(handleUrl, @arrchBuffer, SizeOf(arrchBuffer), dwBytesRead);
               for intNumByte := 0 To dwBytesRead - 1 Do
                 strReadFile := Concat(strReadFile, arrchBuffer[intNumByte]);

               if comProgressBar <> nil then
                 comProgressBar.Position := Form1.ProgressBar1.Position + StrToInt(IntToStr(dwBytesRead));


               if comLabel <> nil then
               comLabel.Caption := FormatFloat('0,000', Length(strReadFile)) + ' od ' + FormatFloat('0,000', dwFileSize) + ' bytes';

               Application.ProcessMessages;
             until dwBytesRead = 0;


           If Length(strReadFile) > 0 Then
             begin
               If FileExists(strDestFile) Then
                 DeleteFile(PChar(strDestFile));

               with TFileStream.Create(strDestFile, fmCreate) Do
                 try
                   Write(strReadFile[1], Length(strReadFile));
                 finally
                   Free;
                 end;
               result := True;
             end
           else
             result := False;
          end;
      end;
  finally
    InternetCloseHandle(handleUrl);
    InternetCloseHandle(handleNet);
  end;
end;
Re: Download problem u delphiu 24.09.2005. u 13:34 · pre 256 meseci #10
Kao sto rekoh... pogledaj kod za TAutoUpdate i sve ce ti biti jasno. Koristi se TDownLoadURL komponenta koja ide sa Delphi 7.
Potpis
Play Store
Re: Download problem u delphiu 24.09.2005. u 13:42 · pre 256 meseci #11
da upravo sam je skino samo sto ne znam kako se koristi. ima dve onprogress eventa ne znam koji je koji...
Re: Download problem u delphiu 24.09.2005. u 13:47 · pre 256 meseci #12
Znam da ne znas kako se koristi... pogledaj kako se koristi TDownLoadURL komponenta. To je ono sto tebi treba.
Potpis
Play Store
Re: Download problem u delphiu 24.09.2005. u 14:22 · pre 256 meseci #13
da mislim da sam skuzio vishe manje. ali ne znam zaglavio sam se. evo sta sam uradio:

Code:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ExtActns, Wininet, ComCtrls, StdCtrls, AutoUpdate;

type
  TForm1 = class(TForm)
    AutoUpdate1: TAutoUpdate;
    ProgressBar1: TProgressBar;
    Button1: TButton;
    procedure AutoUpdate1DownLoadUpdateDataProgress(Sender: TAutoUpdate;
      Progress, ProgressMax: Cardinal; var Cancel: Boolean);
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
implementation

{$R *.dfm}

procedure TForm1.AutoUpdate1DownLoadUpdateDataProgress(Sender: TAutoUpdate;
  Progress, ProgressMax: Cardinal; var Cancel: Boolean);
begin
ProgressBar1.Max:=progressmax;
ProgressBar1.Position:=progress;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
AutoUpdate1.UpdateAddress:='http://users.cjb.net/netwalker/';
AutoUpdate1.UpdateDataFile:='chbook.ini';
AutoUpdate1.DownLoadUpdateData;
end;

end.

e sad ovde u eventu button1click treba da odredim verovatno gde file da ide a ne znam kako?
Re: Download problem u delphiu 24.09.2005. u 18:35 · pre 256 meseci #14
Hehe... nemoj da koristis TAutoUpdate komponentu za obicno skidanje fajla. Koristi samo TDownLoadURL komponentu. Imas help za nju u Delphi-u.
Ako ti treba bas autoupdate funkcija za tvoj program posalji mi pm pa cu ti objasniti kako se koristi.
Potpis
Play Store
Re: Download problem u delphiu 27.09.2005. u 18:16 · pre 256 meseci #15
ako nisi resio problem, evo:

dodas u uses URLMon

i onda ubacis sledecu funkciju:

Code:

function DownloadFile(source, destination: string): boolean;
var
SourceFile, LocalFile: string;
begin
SourceFile := source;
LocalFile := destination;
if URLDownloadToFile(nil, PChar(SourceFile), PChar(LocalFile), 0, nil) = 0 then
  result := true
else
  result := false;
end;



Pa pozivas:

Code:

if not DownloadFile('http://www.sajt.com/path/to/file.ext','c:\file.ext') then
  showmessage('Ne mogu da skinem fajl')
else
 uradinestosafajlom; // ;)
end;


p.s. mislim da sa ovim ne mozes da 'uhvatis' progress kod downloada...
Potpis
Laravel Srbija.

[NE PRUŽAM PODRŠKU ZA PHP PREKO PRIVATNIH PORUKA!]
Prijavite se da biste odgovorili u ovoj temi.PrijavaRegistracija

Premeštanje teme