Search code examples
c#ftpfilezillapycom

FtpWebRequest to Microcontroller always results in (451) Local error in processing


I'm writing a Windows Forms application in C# that allows a user to connect to a microcontroller and upload program files via FTP.

I can successfully upload program files via Filezilla. But when I run the code below I receive the error "The remote server returned an error: (451) Local error in processing." at the line "ftpstream.Close();" every single time no matter what file I try to upload, regardless of size or file type, and the new file does show up in Filezilla on the FiPy but with a size of 0 bytes.

I've erased all program files on the microcontroller so I know storage space is not the issue and I don't think its a firewall issue since I can upload successfully through Filezilla.

The WebException thrown shows no extra details in the InnerException and the StatusDescription comes back as an empty string.

I posted this question to the forum for the specific microcontroller I'm using but didn't get a solution. Here's the link to that post, it includes FTP logs and some solutions that were tried in the comments: https://forum.pycom.io/topic/6937/fipy-ftp-upload-in-c-returns-451-local-error-in-processing

I've exhausted all google results.

Can anyone help me out?

My code:

            var deviceFilepath = "ftp://192.168.4.1/flash/main.py";
            var appFilepath = "rwis\\main.py";

            string UserId = "micro";
            string Password = "python";

            FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(deviceFilepath);
            ftp.Credentials = new NetworkCredential(UserId, Password);

            ftp.UsePassive = true;
            ftp.Method = WebRequestMethods.Ftp.UploadFile;

            var buffer = File.ReadAllBytes(appFilepath);

            Stream ftpstream = ftp.GetRequestStream();
            ftpstream.Write(buffer, 0, buffer.Length);
            ftpstream.Close();

EDIT:

FTP Log from Filezilla:

2021-04-09 09:36:52 3608 1 Status: Connecting to 192.168.4.1:21...
2021-04-09 09:36:52 3608 1 Status: Connection established, waiting for welcome message...
2021-04-09 09:36:52 3608 1 Response: 220 Micropython FTP Server
2021-04-09 09:36:52 3608 1 Status: Plain FTP is insecure. Please switch to FTP over TLS.
2021-04-09 09:36:52 3608 1 Command: USER micro
2021-04-09 09:36:52 3608 1 Response: 331 
2021-04-09 09:36:52 3608 1 Command: PASS ******
2021-04-09 09:36:52 3608 1 Response: 230 
2021-04-09 09:36:52 3608 1 Status: Server does not support non-ASCII characters.
2021-04-09 09:36:52 3608 1 Status: Logged in
2021-04-09 09:36:52 3608 1 Status: Retrieving directory listing...
2021-04-09 09:36:52 3608 1 Command: PWD
2021-04-09 09:36:52 3608 1 Response: 257 /
2021-04-09 09:36:52 3608 1 Status: Directory listing of "/" successful
2021-04-09 09:36:55 3608 1 Status: Retrieving directory listing of "/flash"...
2021-04-09 09:36:55 3608 1 Command: CWD /flash
2021-04-09 09:36:55 3608 1 Response: 250 
2021-04-09 09:36:55 3608 1 Command: TYPE I
2021-04-09 09:36:55 3608 1 Response: 200 
2021-04-09 09:36:55 3608 1 Command: PASV
2021-04-09 09:36:55 3608 1 Response: 227 (192,168,4,1,7,232)
2021-04-09 09:36:55 3608 1 Command: LIST
2021-04-09 09:36:55 3608 1 Response: 150 
2021-04-09 09:36:56 3608 1 Response: 226 
2021-04-09 09:36:56 3608 1 Status: Directory listing of "/flash" successful
2021-04-09 09:36:59 3608 1 Status: Starting upload of C:\Users\fcons_tzfi6gp\Documents\GitHub\production-assembly-app\bin\Debug\rwis\rwis_config.py
2021-04-09 09:36:59 3608 1 Command: TYPE A
2021-04-09 09:36:59 3608 1 Response: 200 
2021-04-09 09:36:59 3608 1 Command: PASV
2021-04-09 09:36:59 3608 1 Response: 227 (192,168,4,1,7,232)
2021-04-09 09:36:59 3608 1 Command: STOR rwis_config.py
2021-04-09 09:36:59 3608 1 Response: 150 
2021-04-09 09:36:59 3608 1 Response: 226 
2021-04-09 09:36:59 3608 1 Status: File transfer successful, transferred 1,361 bytes in 1 second

FTP Log from my code:

System.Net Information: 0 : [15404] FtpWebRequest#47891719::.ctor(ftp://192.168.4.1/flash/rwis_config.py)
System.Net Information: 0 : [15404] FtpWebRequest#47891719::GetRequestStream(Method=STOR.)
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Created connection from 192.168.4.2:61116 to 192.168.4.1:21.
System.Net Information: 0 : [15404] Associating FtpWebRequest#47891719 with FtpControlStream#16454088
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Received response [220 Micropython FTP Server]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Sending command [USER micro]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Received response [331 ]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Sending command [PASS ********]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Received response [230 ]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Sending command [OPTS utf8 on]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Received response [502 ]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Sending command [PWD]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Received response [257 /]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Sending command [TYPE I]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Received response [200 ]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Sending command [PASV]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Received response [227 (192,168,4,1,7,232)]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Sending command [STOR flash/rwis_config.py]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Received response [150 ]
System.Net Information: 0 : [15404] FtpControlStream#16454088 - Received response [451 ]

Solution

  • I got it to work with FluentFTP. I'm not 100% sure what the issue was, maybe using CWD fixed it, maybe some underlying logic/command in FluentFTP handled it better, either way, the FTP log now more closely matches the one from Filezilla and no more errors!

    My New Code:

            FtpClient client = new FtpClient("ftp://192.168.4.1");
            client.Credentials = new NetworkCredential(UserId, Password);
            client.DataConnectionType = FtpDataConnectionType.PASV;
            client.Connect();
            client.SetWorkingDirectory("/flash");
    
            client.UploadFile(appFilepath, "rwis_config.py", FtpRemoteExists.NoCheck);
    
            client.Disconnect();
    

    New FTP Log:

    # Connect()
    Status:   Connecting to ***:21
    Response: 220 Micropython FTP Server
    Command:  USER ***
    Response: 331 
    Command:  PASS ***
    Response: 230 
    Command:  FEAT
    Response: 211 no-features
    Status:   Text encoding: System.Text.ASCIIEncoding
    Command:  SYST
    Response: 215 UNIX Type: L8
    
    # SetWorkingDirectory("/flash")
    Command:  CWD /flash
    Response: 250 
    
    # UploadFile("rwis\rwis_config.py", "rwis_config.py", NoCheck, False, None)
    
    # OpenWrite("rwis_config.py", Binary)
    Command:  TYPE I
    Response: 200 
    
    # OpenPassiveDataStream(PASV, "STOR rwis_config.py", 0)
    Command:  PASV
    Response: 227 (192,168,4,1,7,232)
    Status:   Connecting to ***:2024
    Command:  STOR rwis_config.py
    Response: 150 
    Status:   Disposing FtpSocketStream...
    Response: 226 
    Status:   Testing connectivity using Socket.Poll()...
    Command:  QUIT
    Response: 221 
    Status:   Disposing FtpSocketStream...