Announcement

Collapse
No announcement yet.

Datei vom FTP Sever holen

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Datei vom FTP Sever holen

    Hallo leibe Leute,

    ich soll mit einem Delphi Programm eine Datei von einem FTP Server holen. Der Dateiname fängt immer mit den selben Buchstaben an und dann kommt ein Datum. z.B. Test080404. Jede Datei hat auf dem FTP Server ja auch noch ein Datum. Nun komme ich aber zu meiner Fragen

    Wie kann ich immer die neueste Datei per FTP aus meine Programm herraus downloaden ?

    AN dem Datum der Datei auf dem FTP Server kann ich ja erkennen, welches die neueste Date ist. Nur wie kann ich mittels Delphi herrausfinden und dann noch downloaden ???

    Cu und Danke Tanja

  • #2
    Bei neueren Delphi-Version sind die Indy-Kompoenten dabei. Ansonsten findest Du die hier:<br>
    http://www.nevrona.com/Indy/download/Files/Indy9.html<p>
    Da sind auch Demos dabei, die sicher auch den FTP-Zugriff behandeln.<p>
    Schöne Grüße, Mario Noac
    Schöne Grüße, Mario

    Comment


    • #3
      Hi

      falls du keine Indys nehmen willst kannst du auch die Unit WinINet und ComCtrls nutzen:
      <pre>
      function FtpDownloadFile(strHost, strUser, strPwd: string;
      Port: Integer; ftpDir, ftpFile, TargetFile: string; ProgressBar: TProgressBar): Boolean;

      function FmtFileSize(Size: Integer): string;
      begin
      if Size >= $F4240 then
      Result := Format('%.2f', [Size / $F4240]) + ' Mb'
      else
      if Size < 1000 then
      Result := IntToStr(Size) + ' bytes'
      else
      Result := Format('%.2f', [Size / 1000]) + ' Kb';
      end;

      const
      READ_BUFFERSIZE = 4096; // or 256, 512, ...
      var
      hNet, hFTP, hFile: HINTERNET;
      buffer: array[0..READ_BUFFERSIZE - 1] of Char;
      bufsize, dwBytesRead, fileSize: DWORD;
      sRec: TWin32FindData;
      strStatus: string;
      LocalFile: file;
      bSuccess: Boolean;
      begin
      Result := False;

      { Open an internet session }
      hNet := InternetOpen('Program_Name', // Agent
      INTERNET_OPEN_TYPE_PRECONFIG, // AccessType
      nil, // ProxyName
      nil, // ProxyBypass
      0); // or INTERNET_FLAG_ASYNC / INTERNET_FLAG_OFFLINE

      {
      Agent contains the name of the application or
      entity calling the Internet functions
      }

      { See if connection handle is valid }
      if hNet = nil then
      begin
      ShowMessage('Unable to get access to WinInet.Dll');
      Exit;
      end;

      { Connect to the FTP Server }
      hFTP := InternetConnect(hNet, // Handle from InternetOpen
      PChar(strHost), // FTP server
      port, // (INTERNET_DEFAULT_FTP_PORT),
      PChar(StrUser), // username
      PChar(strPwd), // password
      INTERNET_SERVICE_FTP, // FTP, HTTP, or Gopher?
      0, // flag: 0 or INTERNET_FLAG_PASSIVE
      0);// User defined number for callback

      if hFTP = nil then
      begin
      InternetCloseHandle(hNet);
      ShowMessage(Format('Host "%s" is not available',[strHost]));
      Exit;
      end;

      { Change directory }
      bSuccess := FtpSetCurrentDirectory(hFTP, PChar(ftpDir));

      if not bSuccess then
      begin
      InternetCloseHandle(hFTP);
      InternetCloseHandle(hNet);
      ShowMessage(Format('Cannot set directory to %s.',[ftpDir]));
      Exit;
      end;

      { Read size of file }
      if FtpFindFirstFile(hFTP, PChar(ftpFile), sRec, 0, 0) <> nil then
      begin
      fileSize := sRec.nFileSizeLow;
      // fileLastWritetime := sRec.lastWriteTime
      end else
      begin
      InternetCloseHandle(hFTP);
      InternetCloseHandle(hNet);
      ShowMessage(Format('Cannot find file ',[ftpFile]));
      Exit;
      end;

      { Open the file }
      hFile := FtpOpenFile(hFTP, // Handle to the ftp session
      PChar(ftpFile), // filename
      GENERIC_READ, // dwAccess
      FTP_TRANSFER_TYPE_BINARY, // dwFlags
      0); // This is the context used for callbacks.

      if hFile = nil then
      begin
      InternetCloseHandle(hFTP);
      InternetCloseHandle(hNet);
      Exit;
      end;

      { Create a new local file }
      AssignFile(LocalFile, TargetFile);
      {$i-}
      Rewrite(LocalFile, 1);
      {$i+}

      if IOResult <> 0 then
      begin
      InternetCloseHandle(hFile);
      InternetCloseHandle(hFTP);
      InternetCloseHandle(hNet);
      Exit;
      end;

      dwBytesRead := 0;
      bufsize := READ_BUFFERSIZE;

      while (bufsize > 0) do
      begin
      Application.ProcessMessages;

      if not InternetReadFile(hFile,
      @buffer, // address of a buffer that receives the data
      READ_BUFFERSIZE, // number of bytes to read from the file
      bufsize) then Break; // receives the actual number of bytes read

      if (bufsize > 0) and (bufsize <= READ_BUFFERSIZE) then
      BlockWrite(LocalFile, buffer, bufsize);
      dwBytesRead := dwBytesRead + bufsize;

      { Show Progress }
      ProgressBar.Position := Round(dwBytesRead * (100 / fileSize));
      Form1.Label1.Caption := Format('%s of %s / %d %%',[FmtFileSize(dwBytesRead),FmtFileSize(fileSize) ,ProgressBar.Position]);
      end;

      CloseFile(LocalFile);

      InternetCloseHandle(hFile);
      InternetCloseHandle(hFTP);
      InternetCloseHandle(hNet);
      Result := True;
      end;

      Dann gibts noch die UrlMon-, ComCtrls-, ActiveX-Methode:

      { Private-Deklarationen }
      function OnStartBinding(dwReserved: DWORD; pib: IBinding): HResult; stdcall;
      function GetPriority(out nPriority): HResult; stdcall;
      function OnLowResource(reserved: DWORD): HResult; stdcall;
      function OnProgress(ulProgress, ulProgressMax, ulStatusCode: ULONG;
      szStatusText: LPCWSTR): HResult; stdcall;
      function OnStopBinding(hresult: HResult; szError: LPCWSTR): HResult; stdcall;
      function GetBindInfo(out grfBINDF: DWORD; var bindinfo: TBindInfo): HResult; stdcall;
      function OnDataAvailable(grfBSCF: DWORD; dwSize: DWORD; formatetc: PFormatEtc;
      stgmed: PStgMedium): HResult; stdcall;
      function OnObjectAvailable(const iid: TGUID; punk: IUnknown): HResult; stdcall;
      // function DownloadInternetFile(const Source, Destination: String): Boolean;
      public
      { Public-Deklarationen }
      end;

      var
      Form1: TForm1;

      implementation

      {$R *.dfm}

      // -----------------------------------------------------------------------------

      function TForm1.OnStartBinding(dwReserved: DWORD; pib: IBinding): HResult; stdcall;
      begin
      Result := E_NOTIMPL;
      end; {end function}

      // -----------------------------------------------------------------------------

      function TForm1.GetPriority(out nPriority): HResult; stdcall;
      begin
      Result := E_NOTIMPL;
      end; {end function}

      // -----------------------------------------------------------------------------

      function TForm1.OnLowResource(reserved: DWORD): HResult; stdcall;
      begin
      Result := E_NOTIMPL;
      end; {end function}

      // -----------------------------------------------------------------------------

      function TForm1.OnStopBinding(hresult: HResult; szError: LPCWSTR)
      : HResult; stdcall;
      begin
      Result := E_NOTIMPL;
      end; {end function}

      // -----------------------------------------------------------------------------

      function TForm1.GetBindInfo(out grfBINDF: DWORD; var bindinfo: TBindInfo)
      : HResult; stdcall;
      begin
      Result := E_NOTIMPL;
      end; {end function}

      // -----------------------------------------------------------------------------

      function TForm1.OnDataAvailable(grfBSCF: DWORD; dwSize: DWORD;
      formatetc: PFormatEtc; stgmed: PStgMedium): HResult; stdcall;
      begin
      Result := E_NOTIMPL;
      end; {end function}

      // -----------------------------------------------------------------------------

      function TForm1.OnObjectAvailable(const iid: TGUID; punk: IUnknown)
      : HResult; stdcall;
      begin
      Result := E_NOTIMPL;
      end; {end function}

      // -----------------------------------------------------------------------------

      function TForm1.OnProgress(ulProgress, ulProgressMax, ulStatusCode: ULONG;
      szStatusText: LPCWSTR): HResult; stdcall;
      begin
      with ProgressBar1 do
      begin
      Max := ulProgressMax;
      Position := ulProgress;
      end; {end with}
      Result := NOERROR;
      end; {end function}

      Function DownloadFile(Source, Dest: string): Boolean;
      Begin
      Try
      Result := UrlDownloadToFile(Nil, PChar(Source), PChar(Dest), 0, Nil) = 0;
      Except
      Result := False;
      End;
      End;

      Oder noch diese hier:

      Auch hier wieder die ComCtrls zu verwenden:

      private
      { Private declarations }
      procedure URL_OnDownloadProgress
      (Sender: TDownLoadURL;
      Progress, ProgressMax: Cardinal;
      StatusCode: TURLDownloadStatus;
      StatusText: String; var Cancel: Boolean);
      procedure DoDownload;
      public
      { Public declarations }
      end;

      var
      Form1: TForm1;

      implementation

      {$R *.dfm}

      procedure TForm1.URL_OnDownloadProgress;
      begin
      ProgressBar1.Max:= ProgressMax;
      ProgressBar1.Position:= Progress;
      end;

      procedure TForm1.DoDownload;
      begin
      with TDownloadURL.Create(self) do
      try
      URL := 'http://Domain.us/FileName.Ext';
      FileName := 'c:\FileName.Ext';
      OnDownloadProgress := URL_OnDownloadProgress;

      ExecuteTarget(nil);
      finally
      Free;
      end;
      end;

      procedure TForm1.Button1Click(Sender: TObject);
      begin
      DoDownload;
      end;
      </pre>
      Ich denke hier ist ganz sicher was dabei für dich!!

      Comment

      Working...
      X