WinSCP.SessionRemoteException: 复制文件到远端失败。无法更改为“/ftp:/”; 'ftp:' 未找到

WinSCP.SessionRemoteException: Copying files to remote side failed. Could not change to "/ftp:/"; 'ftp:' not found

enter code here我在将文件放在远程 FTP 服务器上时遇到问题。

我在 C# 中使用 WinSCP .NET 程序集。

我可以连接到 FTP 服务器,但是当我尝试使用 session.PutFile(local,remote) 时,它会跳转到 transferResult.Check() 并出现错误:

{WinSCP.SessionRemoteException: Error transferring file 'C:\testfile.txt. ---> WinSCP.SessionRemoteException: Copying files to remote side failed. Could not change to "/ftp:/"; 'ftp:' not found.

请在下面找到我的代码。

private static void transferWinSCP()
{
    string userName = myUsername;
    string password = myPassword;
    string path = ftp://hostname.com/f/fofd671e-0eee-4c23-932d-4ba32c938c8f;
    string logPath = ConfigurationManager.AppSettings["LogPath"];**
    try
    {
        SessionOptions sessionOptions = new SessionOptions
        {
            Protocol = Protocol.Ftp,
            HostName = "hostname.com",
            PortNumber = 990,
            UserName = userName,
            Password = password,
            FtpSecure = FtpSecure.Implicit,
            FtpMode = FtpMode.Passive,
            TlsHostCertificateFingerprint = "xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
        };

        using (Session session = new Session())
        {

            session.SessionLogPath = logPath;

            //connect
            session.Open(sessionOptions);

            //upload
            TransferOptions transferOptions = new TransferOptions {TransferMode = TransferMode.Binary};

            TransferOperationResult transferResult = session.PutFiles(@"C:\testfile.txt",url, false,
                    transferOptions);
            transferResult.Check(); //ftp error

            foreach (TransferEventArgs transfer in transferResult.Transfers)
            {
                Console.Write("upload of {0} succeeded", transfer.FileName);
            }
        }
    }
    catch (Exception ex)
    {
        Console.Write("error {0}", ex);
    }
}

这是我的日志文件:

. 2015-08-07 10:43:11.653 --------------------------------------------------------------------------
. 2015-08-07 10:43:11.653 WinSCP Version 5.7.4 (Build 5553) (OS 6.1.7601 Service Pack 1 - Windows 7 Professional)
. 2015-08-07 10:43:11.653 Configuration: nul
. 2015-08-07 10:43:11.653 Log level: Normal
. 2015-08-07 10:43:11.653 Local account: userDomain\userUserName
. 2015-08-07 10:43:11.653 Process ID: 30816
. 2015-08-07 10:43:11.653 Command-line: " /xmllog="C:\..\..\Local\Temp\wscp2A00.01E1F1E1.tmp" /xmlgroups /nointeractiveinput /dotnet=574  /ini=nul /log="C:\Log.txt"  /console /consoleinstance=_10752_27784591_85
. 2015-08-07 10:43:11.653 Time zone: Current: GMT-4, Standard: GMT-5 (Eastern Standard Time), DST: GMT-4 (Eastern Daylight Time), DST Start: 3/8/2015, DST End: 11/1/2015
. 2015-08-07 10:43:11.653 Login time: Friday, August 07, 2015 10:43:11 AM
. 2015-08-07 10:43:11.653 --------------------------------------------------------------------------
. 2015-08-07 10:43:11.653 Script: Retrospectively logging previous script records:
> 2015-08-07 10:43:11.653 Script: option batch on
< 2015-08-07 10:43:11.653 Script: batch           on        
< 2015-08-07 10:43:11.653 Script: reconnecttime   120       
> 2015-08-07 10:43:11.653 Script: option confirm off
< 2015-08-07 10:43:11.653 Script: confirm         off       
> 2015-08-07 10:43:11.653 Script: option reconnecttime 120
< 2015-08-07 10:43:11.653 Script: reconnecttime   120       
> 2015-08-07 10:43:11.653 Script: open ftp://domainName%2F---%40.com:***@hostName.com:990 -implicit -certificate="xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx" -passive=1 -timeout=15
. 2015-08-07 10:43:11.653 --------------------------------------------------------------------------
. 2015-08-07 10:43:11.653 Session name: domain\username@hostName.com (Ad-Hoc site)
. 2015-08-07 10:43:11.653 Host name: hostName.com (Port: 990)
. 2015-08-07 10:43:11.654 User name: domainName/username (Password: Yes, Key file: No)
. 2015-08-07 10:43:11.654 Transfer Protocol: FTP
. 2015-08-07 10:43:11.654 Ping type: C, Ping interval: 30 sec; Timeout: 15 sec
. 2015-08-07 10:43:11.654 Disable Nagle: No
. 2015-08-07 10:43:11.654 Proxy: none
. 2015-08-07 10:43:11.654 Send buffer: 262144
. 2015-08-07 10:43:11.654 UTF: 2
. 2015-08-07 10:43:11.654 FTP: FTPS: Implicit TLS/SSL; Passive: Yes [Force IP: A]; MLSD: A [List all: A]
. 2015-08-07 10:43:11.654 Session reuse: Yes
. 2015-08-07 10:43:11.654 TLS/SSL versions: TLSv1.0-TLSv1.2
. 2015-08-07 10:43:11.654 Local directory: default, Remote directory: home, Update: Yes, Cache: Yes
. 2015-08-07 10:43:11.654 Cache directory changes: Yes, Permanent: Yes
. 2015-08-07 10:43:11.654 Timezone offset: 0h 0m
. 2015-08-07 10:43:11.654 --------------------------------------------------------------------------
. 2015-08-07 10:43:11.928 Connecting to hostName.com:990 ...
. 2015-08-07 10:43:11.969 Connected with hostName.com:990, negotiating TLS connection...
. 2015-08-07 10:43:12.040 Verifying certificate for "" with fingerprint xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx and 20 failures
. 2015-08-07 10:43:12.072 Certificate verified against Windows certificate store
. 2015-08-07 10:43:12.072 Certificate common name "*.sharefileftp.com" matches hostname
. 2015-08-07 10:43:12.073 Using TLSv1, cipher TLSv1/SSLv3: RC4-SHA, 2048 bit RSA
. 2015-08-07 10:43:12.073 TLS connection established. Waiting for welcome message...
< 2015-08-07 10:43:12.273 220 ftp-ec2-1.sharefile.com FTP Server Ready (SSL)
> 2015-08-07 10:43:12.273 USER domainName/username
< 2015-08-07 10:43:12.301 331 Hello domainName/username, please enter your password.
> 2015-08-07 10:43:12.301 PASS *********
< 2015-08-07 10:43:12.711 230-Connection established from (unknown) [IPAddress].
< 2015-08-07 10:43:12.938 230-You are connected as username (username).
< 2015-08-07 10:43:12.938 230 Welcome to the Host FTP site.
> 2015-08-07 10:43:12.938 SYST
< 2015-08-07 10:43:13.193 215 UNIX Type: L8
> 2015-08-07 10:43:13.193 FEAT
< 2015-08-07 10:43:13.221 211-Extensions supported:
< 2015-08-07 10:43:13.445  EPSV
< 2015-08-07 10:43:13.445  MDTM
< 2015-08-07 10:43:13.445  PASV
< 2015-08-07 10:43:13.445  REST STREAM
< 2015-08-07 10:43:13.445  SIZE
< 2015-08-07 10:43:13.445  UTF8
< 2015-08-07 10:43:13.445  PBSZ
< 2015-08-07 10:43:13.445  PROT
< 2015-08-07 10:43:13.445  X-NOVELLABS
< 2015-08-07 10:43:13.445  X-CITRIX
< 2015-08-07 10:43:13.445 211 End.
> 2015-08-07 10:43:13.445 OPTS UTF8 ON
< 2015-08-07 10:43:13.472 200 OK.
> 2015-08-07 10:43:13.472 PBSZ 0
< 2015-08-07 10:43:13.498 200 OK.
> 2015-08-07 10:43:13.498 PROT P
< 2015-08-07 10:43:13.526 200 Data connections set to secure (SSL) mode
. 2015-08-07 10:43:13.526 Connected
. 2015-08-07 10:43:13.526 --------------------------------------------------------------------------
. 2015-08-07 10:43:13.526 Using FTP protocol.
. 2015-08-07 10:43:13.526 Doing startup conversation with host.
> 2015-08-07 10:43:13.526 PWD
< 2015-08-07 10:43:13.554 257 "/"
. 2015-08-07 10:43:13.554 Getting current directory name.
. 2015-08-07 10:43:13.554 Startup conversation with host finished.
< 2015-08-07 10:43:13.554 Script: Active session: [1] domain/username@hostFile.com
> 2015-08-07 10:43:13.684 Script: pwd
< 2015-08-07 10:43:13.684 Script: /
> 2015-08-07 10:43:15.532 Script: put  -nopermissions -preservetime -transfer="binary" -- "C:\testFile.txt" "/f/fofd671e-0eee-4c23-932d-4ba32c938c8f/"
. 2015-08-07 10:43:15.534 Copying 1 files/directories to remote directory "/f/fofd671e-0eee-4c23-932d-4ba32c938c8f/"
. 2015-08-07 10:43:15.534   PrTime: Yes; PrRO: No; Rght: rw-r--r--; PrR: No (No); FnCs: N; RIC: 0100; Resume: S (102400); CalcS: No; Mask: 
. 2015-08-07 10:43:15.534   TM: B; ClAr: No; RemEOF: No; RemBOM: No; CPS: 0; NewerOnly: No; InclM: ; ResumeL: 0
. 2015-08-07 10:43:15.534   AscM: *.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; *.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml
. 2015-08-07 10:43:15.535 File: 'C:\testFile.txt' [2015-03-31T18:55:35.408Z] [35]
. 2015-08-07 10:43:15.536 Copying "C:\testFile.txt" to remote directory started.
. 2015-08-07 10:43:15.536 Binary transfer mode selected.
. 2015-08-07 10:43:15.536 Starting upload of C:\testFile.txt
> 2015-08-07 10:43:15.536 CWD /f/fofd671e-0eee-4c23-932d-4ba32c938c8f/
< 2015-08-07 10:43:15.730 550 Could not change to "/f/fofd671e-0eee-4c23-932d-4ba32c938c8f/"; 'f' not found.
> 2015-08-07 10:43:15.730 CWD /f/
< 2015-08-07 10:43:15.915 550 Could not change to "/f/"; 'f' not found.
> 2015-08-07 10:43:15.915 CWD /
< 2015-08-07 10:43:16.113 250 "/" is the current directory.
> 2015-08-07 10:43:16.113 MKD f
< 2015-08-07 10:43:16.466 550 Error creating 'f': Insufficient permissions to create this folder here.
> 2015-08-07 10:43:16.466 CWD /f/
< 2015-08-07 10:43:16.654 550 Could not change to "/f/"; 'f' not found.
. 2015-08-07 10:43:16.654 Copying files to remote side failed.
* 2015-08-07 10:43:16.654 (ExtException) **Copying files to remote side failed.**
* 2015-08-07 10:43:16.654 Could not change to "/f/"; 'f' not found.
. 2015-08-07 10:43:16.654 Asking user:
. 2015-08-07 10:43:16.654 Error transferring file 'C:testFile.txt'. ("Copying files to remote side failed.","Could not change to ""/f/""; 'f' not found.")
< 2015-08-07 10:43:16.654 Script: Error transferring file 'C:\testFile.txt'.
< 2015-08-07 10:43:16.654 Script: Copying files to remote side failed.

< 2015-08-07 10:43:16.654 Could not change to "/f/"; 'f' not found.
* 2015-08-07 10:43:16.655 (EScpSkipFile) Error transferring file 'C:\testFile.txt'.
* 2015-08-07 10:43:16.655 Copying files to remote side failed.
* 2015-08-07 10:43:16.655 Could not change to "/f/"; 'f' not found.
. 2015-08-07 10:43:16.655 Script: Failed
> 2015-08-07 10:43:18.436 Script: exit
. 2015-08-07 10:43:18.436 Script: Exit code: 1
. 2015-08-07 10:43:18.436 Disconnected from server

Session.PutFiles methodremotePath 参数是:

Full path to upload the file to. When uploading multiple files, the filename in the path should be replaced with operation mask or omitted (path ends with slash).

你传递的不是路径,而是URL。做什么的?您已经在 SessionOptions.

中指定了您的主机名和协议

仅使用路径。

此外,由于路径是目标文件夹名称(而非目标文件名),因此必须以斜杠(或文件名)结尾。

string path = "/f/fofd671e-0eee-4c23-932d-4ba32c938c8f/";
...
session.PutFiles(@"C:\testfile.txt", path, false, transferOptions);

使用 WinSCP .NET 程序集查看官方 example for uploading files