简体   繁体   English

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

[英]why is my program not uploading file on remote ftp server?

i coded this program to upload a file on server using ftpput api it is not working it runs but file is not delevered! 我编写了此程序,使用ftpput api将文件上传到服务器上,但无法正常运行,但文件未被删除!

here's the code: 这是代码:

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.

检查FtpPutFile返回值(成功时应返回TRUE ),并使用GetLastError获取详细的错误。

And why don't you try a little harder to see what's going on by testing all the return codes to be sure of where it fails? 而且,为什么不通过测试所有返回码来确定失败的地方,来更加努力地查看发生了什么呢?

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;

You don't even know if you got the connection before trying to send the file ... (It's what I got, as expected, when running the code with these site/user/password values) 您甚至不知道在尝试发送文件之前是否已建立连接 ...(这是按预期的方式,使用这些站点/用户/密码值运行代码时得到的)

And if you get past that, you may actually get a detailed explanation as to why ftpPutFile fails, like in this example: 而且,如果您超过了这一点,您实际上可能会得到有关ftpPutFile失败原因的详细说明,例如以下示例:

System Error.  Code: 3.

The system cannot find the path specified.

First of all, rule-out any major errors in your program (or rule them in, if you like) by checking you can FTP that same file with Microsoft's built-in FTP program. 首先,通过检查是否可以使用Microsoft内置的FTP程序通过FTP将该文件排除,来排除程序中的任何主要错误(或根据需要将其排除)。

From the command line, type 在命令行中,键入

FTP ftp.drivehq.com (return)

if this doesn't come back to you with a login prompt, you have problems outside of your Delphi code. 如果仍然没有出现登录提示,这可能是Delphi代码之外的问题。 Either you have limited internet connectivity (maybe port 25, the FTP port, is blocked by your firewall/router) or there is a problem with the FTP address itself. 您的互联网连接受到限制(可能是端口25(FTP端口)已被防火墙/路由器阻止),或者FTP地址本身存在问题。

If you do get a prompt, enter your username and password when asked. 如果确实收到提示,请在询问时输入您的用户名和密码。 Now type 现在输入

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

if that seems to send your file, (type BYE to leave the FTP program, by the way) then your problem is with your Delphi code rather than with the FTP process itself (the other answers here have helpfully pointed out things you need to check in the Delphi code itself). 如果那似乎发送您的文件(顺便说一句,请键入BYE离开FTP程序),那么您的问题出在您的Delphi代码上,而不是FTP进程本身上(此处的其他答案已帮助您指出了需要检查的内容)在Delphi代码本身中)。 If it doesn't seem to send the file, again I suggest you look to resolving that problem before you carve-up your Delphi code. 如果它似乎没有发送文件,我再次建议您在分担Delphi代码之前寻求解决该问题的方法。

When I'm doing any kind of 'online' work like this, I always try to get a separate process for testing the 'other end' of the system, one that doesn't use any of my own code. 当我做这样的任何“在线”工作时,我总是尝试获得一个单独的过程来测试系统的“另一端”,而不使用我自己的任何代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM