Ĺadny brzuch
czesc
Oto cały kod programu do kopiowania plków z serwera. Wiem że można to zrobić prościej ale ten kod pozwala na kopiowanie plików bez podania ich nazwy!
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} uses Wininet; ////////////////////////////////////////////////// function GetInetFile (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); repeat InternetReadFile(hURL, @Buffer,SizeOf(Buffer), BufferLen); BlockWrite(f, Buffer, BufferLen); until BufferLen = 0; CloseFile(f); Result:=True; finally InternetCloseHandle(hURL) end finally InternetCloseHandle(hSession) end end; ////////////////////////////////////////////////////////////// procedure TForm1.Button1Click(Sender: TObject); var FileOnNet, LocalFileName: string; begin //plik kopiowany //jesli podamy zly adres to wyrzuca error 87, jak sie praed tym uchronic??? FileOnNet:='http://wiadomosci.wp.pl/wiadomosc.html?kat=1342&wid=7905727&rfbawp=1126511618. 377&ticaid=12c1'; //miejsce "upuszczenia" pliku LocalFileName:= 'C:\WINDOWS\Pulpit\plik.txt'; if GetInetFile(FileOnNet,LocalFileName)<>True then { ShowMessage('Download successful') else } ShowMessage('xxxx'); end; end.
problem polega na tym że jeśli podamy niewłaściwy adres FileOnNet to wywala mi błąd, jak się tego ustrzec? :)
do sprawdzenia tego kodu wystarczy Form1 i Button1;
z góry dziękuję za pomoc jesteście SUPER!!!
hURL := InternetOpenURL(hSession, PChar(fileURL), nil,0,0,0); if hURL=nil then exit;
Jakie to proste Wielkie dzięki!
chyba jakąś pomroczność miałem
Pozdrawiam:)
zanotowane.pl doc.pisz.pl pdf.pisz.pl zsf.htw.pl
Oto cały kod programu do kopiowania plków z serwera. Wiem że można to zrobić prościej ale ten kod pozwala na kopiowanie plików bez podania ich nazwy!
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) Button1: TButton; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation {$R *.dfm} uses Wininet; ////////////////////////////////////////////////// function GetInetFile (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); repeat InternetReadFile(hURL, @Buffer,SizeOf(Buffer), BufferLen); BlockWrite(f, Buffer, BufferLen); until BufferLen = 0; CloseFile(f); Result:=True; finally InternetCloseHandle(hURL) end finally InternetCloseHandle(hSession) end end; ////////////////////////////////////////////////////////////// procedure TForm1.Button1Click(Sender: TObject); var FileOnNet, LocalFileName: string; begin //plik kopiowany //jesli podamy zly adres to wyrzuca error 87, jak sie praed tym uchronic??? FileOnNet:='http://wiadomosci.wp.pl/wiadomosc.html?kat=1342&wid=7905727&rfbawp=1126511618. 377&ticaid=12c1'; //miejsce "upuszczenia" pliku LocalFileName:= 'C:\WINDOWS\Pulpit\plik.txt'; if GetInetFile(FileOnNet,LocalFileName)<>True then { ShowMessage('Download successful') else } ShowMessage('xxxx'); end; end.
problem polega na tym że jeśli podamy niewłaściwy adres FileOnNet to wywala mi błąd, jak się tego ustrzec? :)
do sprawdzenia tego kodu wystarczy Form1 i Button1;
z góry dziękuję za pomoc jesteście SUPER!!!
hURL := InternetOpenURL(hSession, PChar(fileURL), nil,0,0,0); if hURL=nil then exit;
Jakie to proste Wielkie dzięki!
chyba jakąś pomroczność miałem
Pozdrawiam:)