簡體   English   中英

為什么我的程序沒有在遠程ftp服務器上上傳文件?

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

我編寫了此程序,使用ftpput api將文件上傳到服務器上,但無法正常運行,但文件未被刪除!

這是代碼:

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獲取詳細的錯誤。

而且,為什么不通過測試所有返回碼來確定失敗的地方,來更加努力地查看發生了什么呢?

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.

首先,通過檢查是否可以使用Microsoft內置的FTP程序通過FTP將該文件排除,來排除程序中的任何主要錯誤(或根據需要將其排除)。

在命令行中,鍵入

FTP ftp.drivehq.com (return)

如果仍然沒有出現登錄提示,這可能是Delphi代碼之外的問題。 您的互聯網連接受到限制(可能是端口25(FTP端口)已被防火牆/路由器阻止),或者FTP地址本身存在問題。

如果確實收到提示,請在詢問時輸入您的用戶名和密碼。 現在輸入

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

如果那似乎發送您的文件(順便說一句,請鍵入BYE離開FTP程序),那么您的問題出在您的Delphi代碼上,而不是FTP進程本身上(此處的其他答案已幫助您指出了需要檢查的內容)在Delphi代碼本身中)。 如果它似乎沒有發送文件,我再次建議您在分擔Delphi代碼之前尋求解決該問題的方法。

當我做這樣的任何“在線”工作時,我總是嘗試獲得一個單獨的過程來測試系統的“另一端”,而不使用我自己的任何代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM