代码之家  ›  专栏  ›  技术社区  ›  Omair Iqbal

为什么我的程序没有在远程ftp服务器上上传文件?

  •  0
  • Omair Iqbal  · 技术社区  · 17 年前

    我编写了这个程序,使用ftpputapi在服务器上上传文件。它不起作用,它运行了,但文件没有被删除!

    unit ftp3;
    
    interface
    
    uses
      Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
      Dialogs, StdCtrls,wininet;
    
    type
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
    
      private
        { Private declarations }
      public
        { Public declarations }
      end;
    
    var
      Form1: TForm1;
    
    implementation
    
        {$R *.dfm}
    
        procedure TForm1.Button1Click(Sender: TObject);
    
           var hInet, hConnect: HINTERNET; 
    
        local_file, 
        remote_file,
        user,remote_server,
        pass: pchar;
    
        begin 
        local_file := 'C:\Documents and Settings\Omair\Desktop\loggen.txt';
        remote_file := 'loggen.txt';
        user := 'my user';
        pass := 'my pass';
        remote_server := ' ftp.drivehq.com';
    
        hInet := InternetOpen(0, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
        hConnect := InternetConnect(hInet,
            remote_server, 
            INTERNET_DEFAULT_FTP_PORT,
            user, pass,
            INTERNET_SERVICE_FTP,
            INTERNET_FLAG_PASSIVE,
            0);
    
        ftpPutFile(hConnect, local_file, remote_file, FTP_TRANSFER_TYPE_BINARY, 0);
    
        InternetCloseHandle(hInet);
        InternetCloseHandle(hConnect);
    
        end;   
    
      end.
    
    3 回复  |  直到 17 年前
        1
  •  2
  •   robsoft    17 年前

    FtpPutFile TRUE 关于成功)并获得详细的错误 GetLastError .

        2
  •  4
  •   pingw33n    17 年前

    测试所有返回代码 确定它在哪里失败了?

    procedure TForm1.Button1Click(Sender: TObject);
    var
      hInet, hConnect: HINTERNET;
      local_file, remote_file, user,remote_server, pass: PChar;
    begin
      local_file := 'C:\Documents and Settings\Omair\Desktop\loggen.txt';
      remote_file := 'loggen.txt';
      user := 'my user';
      pass := 'my pass';
      remote_server := ' ftp.drivehq.com';
    
      hInet := InternetOpen(0, INTERNET_OPEN_TYPE_PRECONFIG, 0, 0, 0);
      if hInet = nil then
        RaiseLastOSError;
    
      hConnect := InternetConnect(hInet,
        remote_server,
        INTERNET_DEFAULT_FTP_PORT,
        user, pass,
        INTERNET_SERVICE_FTP,
        INTERNET_FLAG_PASSIVE,
        0);
      if hConnect = nil then
        RaiseLastOSError;
    
      if not ftpPutFile(hConnect, local_file, remote_file, FTP_TRANSFER_TYPE_BINARY, 0) then
        RaiseLastOSError;
    
      if not InternetCloseHandle(hConnect) then
        RaiseLastOSError;
      if not InternetCloseHandle(hInet) then
        RaiseLastOSError;
    end;
    

    你甚至都不知道 在尝试发送文件之前,知道您是否已连接

    如果你克服了这一点,你实际上可能会得到一个关于ftpPutFile失败原因的详细解释,就像这个例子一样:

    System Error.  Code: 3.
    
    The system cannot find the path specified.
    
        3
  •  4
  •   Francesca    17 年前

    首先,通过检查您是否可以使用Microsoft的内置FTP程序对同一文件进行FTP,排除程序中的任何重大错误(或者如果您愿意,可以将其排除在外)。

    在命令行中,键入

    FTP ftp.drivehq.com (return)

    如果您没有收到登录提示,则说明您的Delphi代码之外存在问题。您的互联网连接有限(可能是FTP端口25被防火墙/路由器阻止),或者FTP地址本身有问题。

    BIN (return)
    PUT 'C:\Documents and Settings\Omair\Desktop\loggen.txt' (return)

    如果这似乎发送了你的文件(顺便说一句,键入BYE离开FTP程序),那么你的问题在于你的Delphi代码,而不是FTP进程本身(这里的其他答案很有帮助地指出了你需要在Delphi代码本身中检查的事情)。如果它似乎没有发送文件,我再次建议你在分割Delphi代码之前先解决这个问题。

    当我做任何一种像这样的“在线”工作时,我总是试图得到一个单独的过程来测试系统的“另一端”,一个不使用我自己代码的过程。