AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Netzwerke Delphi ftp download cannot find file ???
Thema durchsuchen
Ansicht
Themen-Optionen

ftp download cannot find file ???

Ein Thema von Blumi · begonnen am 1. Okt 2005 · letzter Beitrag vom 2. Okt 2005
Antwort Antwort
Benutzerbild von Blumi
Blumi

Registriert seit: 11. Jul 2005
Ort: Basel
96 Beiträge
 
Delphi 2005 Personal
 
#1

ftp download cannot find file ???

  Alt 1. Okt 2005, 12:35
Hallo zusammen, habe nochmals eine Frage, die aber etwas komisch ist, nuja.

Ich habe immer noch folgenden Code für den Download:

Delphi-Quellcode:
{
  The following function shows how to connect to a ftp server
  and download a file.
  It uses the functions from wininet.dll.

  You need a ProgressBar to show the progress and a Label to show progress informations.
}


uses
  WinInet, ComCtrls;

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;
Jetzt gibt es aber bei manchen Servern Problemen, keine Ahnung??
Wenn ich den Pfad einer Datei in eine Liste lade zeigt es mir eine Datei an 'examples.pdf '. Wenn ich aber die Datei mit der vorherigen Funktion laden möchte kommt folgende Meldung:

Cannot find File

Das verstehe ich eigentlich nicht, wenn sie icht vorhanden wäre würde sie doch auch nicht in der Liste anzeigen? Oder was mach ich falsch??

Vielen Dank im Vorraus, Gruss Blumi
  Mit Zitat antworten Zitat
marabu

Registriert seit: 6. Apr 2005
10.109 Beiträge
 
#2

Re: ftp download cannot find file ???

  Alt 1. Okt 2005, 15:43
Hi,

die Funktion FtpFindFirstFile() will einen fullname:

Delphi-Quellcode:
...
var
  ftpFullName: string;
...
  if (ftpDir = '') or (ftpDir[Length(ftpDir)] <> '/') then
    ftpDir := ftpDir + '/';
  ftpFullName := ftpDir + ftpFile;

  if FTPFindFirstFile(hFTP, PChar(ftpFullName), sRec, 0, 0) <> nil then
...
Grüße vom marabu
  Mit Zitat antworten Zitat
Benutzerbild von Blumi
Blumi

Registriert seit: 11. Jul 2005
Ort: Basel
96 Beiträge
 
Delphi 2005 Personal
 
#3

Re: ftp download cannot find file ???

  Alt 1. Okt 2005, 18:43
Sry, leider hat es diesmal nicht so richtig funktioniert, besser gesagt es geht gar nichts mehr

Also wenn ich dort nciht ftpFile sondern ftpFullName stehen habe, dann aldet es nciht einmal eine Datei die mit ftpFullName funktioniert !

Ich habe es so eingebaut:

Delphi-Quellcode:

var
  ftpFullName: string;
...


  if (ftpDir = '') or (ftpDir[Length(ftpDir)] <> '/') then
    ftpDir := ftpDir + '/';
  ftpFullName := ftpDir + ftpFile;

  { Read size of file }
  if FtpFindFirstFile(hFTP, PChar(ftpFullName), sRec, 0, 0) <> nil then
  begin
    fileSize := sRec.nFileSizeLow;
    // fileLastWritetime := sRec.lastWriteTime
  end else
  begin
    InternetCloseHandle(hFTP);
    InternetCloseHandle(hNet);

...

Blumi
  Mit Zitat antworten Zitat
marabu

Registriert seit: 6. Apr 2005
10.109 Beiträge
 
#4

Re: ftp download cannot find file ???

  Alt 1. Okt 2005, 19:00
Hallo Blumi,

ich hatte den Code vorher mit meinem Filezilla Server getestet. Funktioniert der Code bei dir denn gar nicht oder nur in Verbindung mit bestimmten Servern?

marabu
  Mit Zitat antworten Zitat
Benutzerbild von Blumi
Blumi

Registriert seit: 11. Jul 2005
Ort: Basel
96 Beiträge
 
Delphi 2005 Personal
 
#5

Re: ftp download cannot find file ???

  Alt 1. Okt 2005, 19:44
Hi

Also ich habe es auch mit Servern probiert die vorher funktionierten, und jetzt geht gar nichts mehr, es kommt immer die Fehlermeldung File Not Found
  Mit Zitat antworten Zitat
marabu

Registriert seit: 6. Apr 2005
10.109 Beiträge
 
#6

Re: ftp download cannot find file ???

  Alt 2. Okt 2005, 09:53
Hallo Blumi,

kannst du mir einen public ftp server nennen, bei dem mein Code versagt? Ich könnte das dann näher untersuchen.

marabu
  Mit Zitat antworten Zitat
Benutzerbild von Blumi
Blumi

Registriert seit: 11. Jul 2005
Ort: Basel
96 Beiträge
 
Delphi 2005 Personal
 
#7

Re: ftp download cannot find file ???

  Alt 2. Okt 2005, 14:18
Hi

Nach mehrmailigem Ausprobieren habe ich es jetzt herausgefunden, wann es nicht geht. Uns zwar wenn der Dateiname ein Abstand enthält, Beispiel: "1 Zeichnen ... .pdf" !!

Würd mich freuen wenn du es heruasbekommen könntest, wie ich das fertig bringe, habe dir den FTP geschickt,


Gruss
  Mit Zitat antworten Zitat
marabu

Registriert seit: 6. Apr 2005
10.109 Beiträge
 
#8

Re: ftp download cannot find file ???

  Alt 2. Okt 2005, 15:06
Dann hat sich ja alles aufgeklärt - Leerstellen im Namen gestattet die von dir verwendete Funktion nicht:

Zitat von PSDK:
lpszSearchFile
[in] Pointer to a null-terminated string that specifies a valid directory path or file name for the FTP server's file system. The string can contain wildcards, but no blank spaces are allowed. If the value of lpszSearchFile is NULL or if it is an empty string, the function finds the first file in the current directory on the server.
Für solche Namen wirst du einen wildcard character einsetzen und dann über die gefundenen Dateien iterieren müssen, um im direkten Vergleich dann die richtige Datei zu identifizieren.

marabu
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 22:48 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz