📄 jvftpgrabber.pas
字号:
{-----------------------------------------------------------------------------
The contents of this file are subject to the Mozilla Public License
Version 1.1 (the "License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.mozilla.org/MPL/MPL-1.1.html
Software distributed under the License is distributed on an "AS IS" basis,
WITHOUT WARRANTY OF ANY KIND, either expressed or implied. See the License for
the specific language governing rights and limitations under the License.
The Original Code is: JvFTPGrabber.PAS, released on 2001-02-28.
The Initial Developer of the Original Code is S閎astien Buysse [sbuysse att buypin dott com]
Portions created by S閎astien Buysse are Copyright (C) 2001 S閎astien Buysse.
All Rights Reserved.
Contributor(s): Michael Beck [mbeck att bigfoot dott com].
Alejandro Castro [alejandro@alfra.info].
Last Modified: 2000-02-28 / 2002-10-18
You may retrieve the latest version of this file at the Project JEDI home page,
located at http://www.delphi-jedi.org
Known Issues:
-----------------------------------------------------------------------------}
{$I jvcl.inc}
{$HPPEMIT '#pragma link "wininet.lib"'}
unit JvFTPGrabber;
interface
uses
Windows, SysUtils, Classes,
JvTypes, JvComponent;
type
TJvDownloadMode = (hmBinary, hmAscii);
TJvFtpThread = class(TThread)
private
FSender: TObject; //acp
FStream: TMemoryStream;
FUrl: string;
FPassiveFTP: Boolean;
FUserName: string;
FFileName: string;
FPassword: string;
FOutputMode: TJvOutputMode;
FOnError: TJvErrorEvent;
FOnDoneFile: TJvDoneFileEvent;
FOnDoneStream: TJvDoneStreamEvent;
FOnProgress: TJvFTPProgressEvent;
FMode: TJvDownloadMode;
FAgent: string;
FBytesRead: Integer;
FErrorText: string;
FOnStatus: TJvFTPProgressEvent;
FOnClosed: TNotifyEvent;
function GetLastErrorMsg: string;
protected
procedure Error;
procedure Progress;
procedure Ended;
procedure Execute; override;
procedure Closed;
public
constructor Create(Url, UserName, FileName, Password: string;
OutputMode: TJvOutputMode; PassiveFTP: Boolean; AOnError: TJvErrorEvent;
AOnDoneFile: TJvDoneFileEvent; AOnDoneStream: TJvDoneStreamEvent;
AOnProgress: TJvFTPProgressEvent; Mode: TJvDownloadMode; Agent: string;
AOnStatus: TJvFTPProgressEvent; Sender: TObject; AOnClosedConnection: TNotifyEvent); // acp
end;
TJvFTPGrabber = class(TJvComponent)
private
FSize: Integer; // acp
FThread: TJvFtpThread;
FUrl: string;
FUserName: string;
FFileName: TFileName;
FPassword: string;
FOutputMode: TJvOutputMode;
FOnError: TJvErrorEvent;
FOnDoneFile: TJvDoneFileEvent;
FOnDoneStream: TJvDoneStreamEvent;
FOnProgress: TJvFTPProgressEvent;
FMode: TJvDownloadMode;
FAgent: string;
FOnReceived: TNotifyEvent;
FOnResolving: TNotifyEvent;
FOnConnecting: TNotifyEvent;
FOnConnected: TNotifyEvent;
FOnResolved: TNotifyEvent;
FOnRedirect: TNotifyEvent;
FOnSent: TNotifyEvent;
FOnSending: TNotifyEvent;
FOnStateChange: TNotifyEvent;
FOnReceiving: TNotifyEvent;
FOnClosed: TNotifyEvent;
FOnClosing: TNotifyEvent;
FOnRequest: TNotifyEvent;
FPassiveFTP: Boolean;
procedure ThreadFinished(Sender: TObject);
procedure Error(Sender: TObject; ErrorMsg: string);
procedure DoneFile(Sender: TObject; FileName: string; FileSize: Integer; Url: string);
procedure DoneStream(Sender: TObject; Stream: TStream; StreamSize: Integer; Url: string);
procedure Progress(Sender: TObject; Position: Integer; Url: string);
procedure Status(Sender: TObject; Position: Integer; Url: string);
procedure Closed(Sender: TObject);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
published
property Size: Integer read FSize write FSize; // acp
property Url: string read FUrl write FUrl;
property UserName: string read FUserName write FUserName;
property Password: string read FPassword write FPassword;
property FileName: TFileName read FFileName write FFileName;
property OutputMode: TJvOutputMode read FOutputMode write FOutputMode default omStream;
property PassiveFTP: Boolean read FPassiveFTP write FPassiveFTP;
property Mode: TJvDownloadMode read FMode write FMode default hmBinary;
property Agent: string read FAgent write FAgent;
property OnDoneFile: TJvDoneFileEvent read FOnDoneFile write FOnDoneFile;
property OnDoneStream: TJvDoneStreamEvent read FOnDoneStream write FOnDoneStream;
property OnError: TJvErrorEvent read FOnError write FOnError;
property OnProgress: TJvFTPProgressEvent read FOnProgress write FOnProgress;
property OnResolvingName: TNotifyEvent read FOnResolving write FOnResolving;
property OnResolvedName: TNotifyEvent read FOnResolved write FOnResolved;
property OnConnectingToServer: TNotifyEvent read FOnConnecting write FOnConnecting;
property OnConnectedToServer: TNotifyEvent read FOnConnected write FOnConnected;
property OnSendingRequest: TNotifyEvent read FOnSending write FOnSending;
property OnRequestSent: TNotifyEvent read FOnSent write FOnSent;
property OnReceivingResponse: TNotifyEvent read FOnReceiving write FOnReceiving;
property OnReceivedResponse: TNotifyEvent read FOnReceived write FOnReceived;
property OnClosingConnection: TNotifyEvent read FOnClosing write FOnClosing;
property OnClosedConnection: TNotifyEvent read FOnClosed write FOnClosed;
property OnRequestComplete: TNotifyEvent read FOnRequest write FOnRequest;
property OnRedirect: TNotifyEvent read FOnRedirect write FOnRedirect;
property OnStateChange: TNotifyEvent read FOnStateChange write FOnStateChange;
procedure Execute;
procedure Terminate;
end;
implementation
uses
WinInet;
{$IFNDEF COMPILER6_UP}
function FtpGetFileSize(hFile: HINTERNET; lpdwFileSizeHigh: LPDWORD): DWORD; stdcall;
external 'wininet.dll' name 'FtpGetFileSize';
{$ENDIF COMPILER6_UP}
//=== TJvFTPGrabber ==========================================================
constructor TJvFTPGrabber.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FUrl := '';
FUserName := '';
FPassword := '';
FFileName := '';
FOutputMode := omStream;
FMode := hmBinary;
FAgent := 'TJvHttpGrabber Component';
FThread := nil;
FSize := 0;
end;
destructor TJvFTPGrabber.Destroy;
begin
if FThread <> nil then
begin
FThread.FreeOnTerminate := True;
FThread.Terminate;
end;
inherited Destroy;
end;
procedure TJvFTPGrabber.Terminate; // acp
begin
if Assigned(FThread) then
FThread.Terminate;
end;
procedure TJvFTPGrabber.DoneFile(Sender: TObject; FileName: string;
FileSize: Integer; Url: string);
begin
if Assigned(FOnDoneFile) then
FOnDoneFile(Self, FileName, FileSize, Url);
end;
procedure TJvFTPGrabber.DoneStream(Sender: TObject; Stream: TStream;
StreamSize: Integer; Url: string);
begin
if Assigned(FOnDoneStream) then
FOnDoneStream(Self, Stream, StreamSize, Url);
end;
procedure TJvFTPGrabber.Error(Sender: TObject; ErrorMsg: string);
begin
if Assigned(FOnError) then
FOnError(Self, ErrorMsg);
end;
procedure TJvFTPGrabber.Closed(Sender: TObject);
begin
if Assigned(FOnClosed) then
FOnClosed(Self);
end;
procedure TJvFTPGrabber.Execute;
begin
//Download it
if FThread = nil then
begin
FThread := TJvFtpThread.Create(Url, UserName, FileName, Password, OutputMode,
PassiveFTP, Error, DoneFile, DoneStream, Progress, Mode, Agent,
Status, Self, Closed); // acp
FThread.OnTerminate := ThreadFinished;
FThread.Resume;
end;
end;
procedure TJvFTPGrabber.Progress(Sender: TObject; Position: Integer; Url: string);
begin
if Assigned(FOnProgress) then
FOnProgress(Self, Position, Url);
end;
procedure TJvFTPGrabber.Status(Sender: TObject; Position: Integer; Url: string);
begin
case Position of
INTERNET_STATUS_RESOLVING_NAME:
if Assigned(FOnResolving) then
FOnResolving(Self);
INTERNET_STATUS_NAME_RESOLVED:
if Assigned(FOnResolved) then
FOnResolved(Self);
INTERNET_STATUS_CONNECTING_TO_SERVER:
if Assigned(FOnConnecting) then
FOnConnecting(Self);
INTERNET_STATUS_CONNECTED_TO_SERVER:
if Assigned(FOnConnected) then
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -