我正在用 C# 编写一个 Windows 窗体应用程序,它允许用户连接到微控制器并通过 FTP 上传程序文件。
我可以通过 Filezilla 成功上传程序文件。但是当我运行下面的代码时,我收到错误“远程服务器返回错误:(451)处理中的本地错误。” 在“ftpstream.Close();”行 每次无论我尝试上传什么文件,无论大小或文件类型如何,新文件都会显示在 FiPy 上的 Filezilla 中,但大小为 0 字节。
我已经删除了微控制器上的所有程序文件,所以我知道存储空间不是问题,我不认为这是防火墙问题,因为我可以通过 Filezilla 成功上传。
抛出的 WebException 在 InnerException 中没有显示额外的详细信息,并且 StatusDescription 作为空字符串返回。
我在论坛上针对我正在使用的特定微控制器发布了这个问题,但没有得到解决方案。这是该帖子的链接,它包括 FTP 日志和在评论中尝试过的一些解决方案: https ://forum.pycom.io/topic/6937/fipy-ftp-upload-in-c-returns-451-local -处理中的错误
我已经用尽了所有谷歌搜索结果。
谁能帮我吗?
我的代码:
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();
编辑:
Filezilla 的 FTP 日志:
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 日志:
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 ]