Upload fails for plain FTP

Advertisement

eiland_nl
Guest

Upload fails for plain FTP

Trying to upload a file but I wont work with this script, works fine for FTPS or SFTP but normal FTP fails.
. 2023-01-20 11:17:28.923 --------------------------------------------------------------------------
. 2023-01-20 11:17:28.923 Using FTP protocol.
. 2023-01-20 11:17:28.923 Doing startup conversation with host.
< 2023-01-20 11:17:28.923 Script: Starting the session...
> 2023-01-20 11:17:28.923 PWD
< 2023-01-20 11:17:28.931 257 "/" is current directory.
. 2023-01-20 11:17:28.931 Getting current directory name.
. 2023-01-20 11:17:28.931 Startup conversation with host finished.
< 2023-01-20 11:17:28.931 Script: Session started.
< 2023-01-20 11:17:28.932 Script: Active session: [1] cpi225001@ftp.vwe.nl
> 2023-01-20 11:17:29.023 Script: pwd
< 2023-01-20 11:17:29.024 Script: /
> 2023-01-20 11:17:29.074 Script: stat -- "/dir"
. 2023-01-20 11:17:29.074 Listing file "/dir".
. 2023-01-20 11:17:29.075 Retrieving file information...
> 2023-01-20 11:17:29.075 MLST /dir
< 2023-01-20 11:17:29.085 250-Listing /dir
< 2023-01-20 11:17:29.085  type=dir;modify=20230120093518; /dir
< 2023-01-20 11:17:29.085 250 End
. 2023-01-20 11:17:29.085  type=dir;modify=20230120093518; /dir
. 2023-01-20 11:17:29.085 Retrieving file information successful
. 2023-01-20 11:17:29.085 dir;D;0;2023-01-20T09:35:18.000Z;4;"" [0];"" [0];---------;0
< 2023-01-20 11:17:29.086 Script: D---------   0                           0 Jan 20 10:35:18 2023 dir
> 2023-01-20 11:17:29.126 Script: put  -nopermissions -nopreservetime -transfer="automatic" -resumesupport="off" -- "D:\Imports\test.txt" "/dir"
. 2023-01-20 11:17:29.127 Copying 1 files/directories to remote directory "/" - total size: 1
. 2023-01-20 11:17:29.127   PrTime: No; PrRO: No; Rght: rw-r--r--; PrR: No (No); FnCs: N; RIC: 0100; Resume: N (102400); CalcS: No; Mask: dir
. 2023-01-20 11:17:29.127   TM: M; ClAr: No; RemEOF: No; RemBOM: No; CPS: 0; NewerOnly: No; EncryptNewFiles: Yes; ExcludeHiddenFiles: No; ExcludeEmptyDirectories: No; InclM: ; ResumeL: 0
. 2023-01-20 11:17:29.127   AscM: *.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; *.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml
. 2023-01-20 11:17:29.127 File: 'D:\Imports\test.txt' [2023-01-19T14:04:56.546Z] [1]
. 2023-01-20 11:17:29.128 Copying "D:\Imports\test.txt" to remote directory started.
. 2023-01-20 11:17:29.128 Ascii transfer mode selected.
. 2023-01-20 11:17:29.129 Starting upload of D:\Imports\test.txt
> 2023-01-20 11:17:29.129 TYPE A
< 2023-01-20 11:17:29.137 200 Type set to A
> 2023-01-20 11:17:29.137 PASV
< 2023-01-20 11:17:29.144 227 Entering Passive Mode (80,85,160,204,12,241)
> 2023-01-20 11:17:29.145 STOR dir
. 2023-01-20 11:17:29.145 Connecting to 80.85.160.204:3313 ...
< 2023-01-20 11:17:29.160 550 Filename invalid
. 2023-01-20 11:17:29.160 Copying files to remote side failed.
* 2023-01-20 11:17:29.160 (ExtException) **Copying files to remote side failed.**
* 2023-01-20 11:17:29.161 Filename invalid
. 2023-01-20 11:17:29.161 Asking user:
. 2023-01-20 11:17:29.161 Error transferring file 'D:\Imports\test.txt'. ("Copying files to remote side failed.","Filename invalid")
< 2023-01-20 11:17:29.161 Script: Error transferring file 'D:\Imports\test.txt'.
< 2023-01-20 11:17:29.161 Script: Copying files to remote side failed.
 
< 2023-01-20 11:17:29.161 Filename invalid
. 2023-01-20 11:17:29.163 Answer: Abort
* 2023-01-20 11:17:29.164 (ESkipFile) Error transferring file 'D:\Imports\test.txt'.
* 2023-01-20 11:17:29.164 Copying files to remote side failed.
* 2023-01-20 11:17:29.164 Filename invalid
. 2023-01-20 11:17:29.164 Copying finished: Transferred: 0, Elapsed: 0:00:00, CPS: 0/s
. 2023-01-20 11:17:29.164 Script: Failed
> 2023-01-20 11:17:29.177 Script: exit
. 2023-01-20 11:17:29.177 Script: Exit code: 1
. 2023-01-20 11:17:29.178 Disconnected from server

public static SqlString Upload(string transferProtocol, string HostName, Int32 Port, string User, string Password, string SshHostKeyFingerprint, string SourceFileName, string DestPath, string SshPrivateKeyPath, string PrivateKeyPassphrase)
{
    SourceFileName = Environment.ExpandEnvironmentVariables(SourceFileName);
    DestPath = Environment.ExpandEnvironmentVariables(DestPath);
    var protocol = transferProtocol.ToLower() == "ftp" ? Protocol.Ftp : Protocol.Sftp;
    var sourcePath = Path.GetDirectoryName(SourceFileName);
    var logName = Path.Combine(sourcePath, string.Format("Log_{0}_{1}.log", HostName, DateTime.Now.ToString("yyyyMMddHHmmss")));
 
    var sessionOptions = GetSessionOptions(HostName, Port, User, Password, SshHostKeyFingerprint, SshPrivateKeyPath, PrivateKeyPassphrase, protocol);
 
    using (Session session = new Session())
    {
        session.SessionLogPath = logName;
 
        session.Open(sessionOptions);
        if (!session.FileExists(DestPath))
        {
            session.CreateDirectory(DestPath);
        }
 
        var transferOptions = new TransferOptions
        {
            TransferMode = TransferMode.Binary ,
            PreserveTimestamp = false
        };
        transferOptions.ResumeSupport.State = TransferResumeSupportState.Off;
        var transferResult = session.PutFiles(Path.GetFullPath(SourceFileName), DestPath, false, transferOptions);
 
        if (!transferResult.IsSuccess)
        {
            return (string.Format("Error:Transfer failed for {0} to {1}: {2}", SourceFileName, HostName, transferResult.Failures[0].Message));
        }
    }
    File.Delete(logName);
    return (string.Format("{0} is uploaded to {1}{2}", SourceFileName, HostName, DestPath));
}

This is the log when i upload the file from WinSCP UI.
. 2023-01-20 11:32:27.791 Size of 1 local files/folders calculated as 0
. 2023-01-20 11:32:27.834 Copying 1 files/directories to remote directory "/KVKBestand/" - total size: 0
. 2023-01-20 11:32:27.834   PrTime: Yes; PrRO: No; Rght: rw-r--r--; PrR: No (No); FnCs: N; RIC: 0100; Resume: S (102400); CalcS: Yes; Mask: *.*
. 2023-01-20 11:32:27.834   TM: B; ClAr: No; RemEOF: No; RemBOM: No; CPS: 0; NewerOnly: No; EncryptNewFiles: Yes; ExcludeHiddenFiles: No; ExcludeEmptyDirectories: No; InclM: ; ResumeL: 0
. 2023-01-20 11:32:27.834   AscM: *.*html; *.htm; *.txt; *.php; *.php3; *.cgi; *.c; *.cpp; *.h; *.pas; *.bas; *.tex; *.pl; *.js; .htaccess; *.xtml; *.css; *.cfg; *.ini; *.sh; *.xml
. 2023-01-20 11:32:27.835 File: 'C:\Users\adminlangras\Desktop\text.txt' [2023-01-20T10:31:55.351Z] [0]
. 2023-01-20 11:32:27.835 Copying "C:\Users\adminlangras\Desktop\text.txt" to remote directory started.
. 2023-01-20 11:32:27.836 Binary transfer mode selected.
. 2023-01-20 11:32:27.836 Starting upload of C:\Users\adminlangras\Desktop\text.txt
> 2023-01-20 11:32:27.836 TYPE I
< 2023-01-20 11:32:27.842 200 Type set to I
> 2023-01-20 11:32:27.842 PASV
< 2023-01-20 11:32:27.851 227 Entering Passive Mode (80,85,160,204,12,254)
> 2023-01-20 11:32:27.851 STOR text.txt
. 2023-01-20 11:32:27.851 Connecting to 80.85.160.204:3326 ...
< 2023-01-20 11:32:27.864 150 Opening data channel for file upload to server of "/KVKBestand/text.txt"
< 2023-01-20 11:32:27.870 226 Successfully transferred "/KVKBestand/text.txt"
> 2023-01-20 11:32:27.870 MFMT 20230120103155 text.txt
< 2023-01-20 11:32:27.877 213 modify=20230120103155; /KVKBestand/text.txt
. 2023-01-20 11:32:27.877 Upload successful
. 2023-01-20 11:32:27.877 Got reply 1 to the command 4
. 2023-01-20 11:32:27.877 Transfer done: 'C:\Users\adminlangras\Desktop\text.txt' => '/KVKBestand/text.txt' [0]
. 2023-01-20 11:32:27.877 Copying finished: Transferred: 0, Elapsed: 0:00:00, CPS: 0/s
. 2023-01-20 11:32:27.877 Retrieving directory listing...
> 2023-01-20 11:32:27.877 TYPE A
< 2023-01-20 11:32:27.884 200 Type set to A
> 2023-01-20 11:32:27.885 PASV
< 2023-01-20 11:32:27.891 227 Entering Passive Mode (80,85,160,204,12,232)
> 2023-01-20 11:32:27.891 MLSD
. 2023-01-20 11:32:27.891 Connecting to 80.85.160.204:3304 ...
. 2023-01-20 11:32:27.904 type=file;modify=20230120103155;size=0; text.txt
. 2023-01-20 11:32:27.904 Data connection closed
< 2023-01-20 11:32:27.904 150 Opening data channel for directory listing of "/KVKBestand"
< 2023-01-20 11:32:27.904 226 Successfully transferred "/KVKBestand"
. 2023-01-20 11:32:27.904 Directory listing successful
. 2023-01-20 11:32:27.904 Got reply 1 to the command 2
. 2023-01-20 11:32:27.904 ..;D;0;1899-12-30T01:00:00.000Z;0;"" [0];"" [0];---------;0
. 2023-01-20 11:32:27.904 text.txt;-;0;2023-01-20T10:31:55.000Z;4;"" [0];"" [0];---------;0

Reply with quote

Advertisement

martin
Site Admin
martin avatar
Joined:
Posts:
41,517
Location:
Prague, Czechia

Re: Upload fails for plain FTP

I assume that your DestPath value does not have the correct syntax.
It should be like /remote/path/dir/ or /remote/path/dir/*, while you are probably using /remote/path/dir.
See https://winscp.net/eng/docs/library_session_putfiles#remotepath
and https://winscp.net/eng/docs/faq_script_vs_gui#inputs

Alternatively, use Session.PutFilesToDirectory, which is not that strict:
session.PutFileToDirectory(Path.GetFullPath(SourceFileName), DestPath, false, transferOptions);

If this doe not help, please post more specific example with actual paths, not just variables. And code and log for your working FTPS session.

Reply with quote

Advertisement

You can post new topics in this forum