我正在尝试使用 C# 将文件上传到 FTP 服务器。除了我的所有文件(一旦在服务器上)都有页眉和页脚这一事实之外,上传工作正常。这里有一个例子:--------------8d5fde16cb03f95Content-Disposition: form-data; name="file"; filename="PNPP190618.manifest"Content-Type: application/octet-stream<Real file content here>--------------8d5fde16cb03f95当然我检查了我的原始文件没有这个文本,我上传了一些有同样问题的二进制文件。谢谢你。编辑:忘记向您展示我的代码: WebClient client = new System.Net.WebClient(); Uri uri = new Uri(FTPHost + new FileInfo(FilePath).Name); client.UploadProgressChanged += new UploadProgressChangedEventHandler(OnFileUploadProgressChanged); client.UploadFileCompleted += new UploadFileCompletedEventHandler(OnFileUploadCompleted); client.Credentials = new System.Net.NetworkCredential(FTPUserName, FTPPassword); client.UploadFileAsync(uri, "STOR", FilePath);EDIT2:WinSCP 会话日志(此方法没有问题):https : //pastebin.com/sv7FNC0iEDIT3:我没有使用代理。我试图在 Unity 2017 (Mono .NET 3.5) 和控制台项目 (.NET Framework 3.5) 的最小示例上重现该问题。.NET Framework 没有问题,但在 Unity 2017 中有问题。以下是重现最小 Unity 项目的步骤:创建一个新的空白 Unity 项目在场景中添加一个空的 GameObject,并添加一个新脚本粘贴下面的代码并将类名替换为您的脚本文件名用文件和FTP服务器的示例填充属性按播放using System;using System.IO;using System.Net;using UnityEngine;public class Test : MonoBehaviour { string FTPHost = "ftp://Fill here"; string FTPUserName = "Fill here"; string FTPPassword = "Fill here"; string FilePath = "Fill here"; // Use this for initialization void Start () { WebClient client = new WebClient(); Uri uri = new Uri(FTPHost + new FileInfo(FilePath).Name); client.Credentials = new NetworkCredential(FTPUserName, FTPPassword); client.UploadFile(uri, WebRequestMethods.Ftp.UploadFile, FilePath); } // Update is called once per frame void Update () { }}
1 回答
拉莫斯之舞
TA贡献1820条经验 获得超10个赞
我改用了 FtpWebRequest。代码要复杂得多,但它有效:
FtpState state = new FtpState();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(uri);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential (FTPUserName,FTPPassword);
state.Request = request;
state.FileName = FilePath;
request.BeginGetRequestStream(
new AsyncCallback (EndGetStreamCallback),
state
);
从 MSDN 文档中复制并粘贴 EndGetStreamCallback 和 FtpState。
- 1 回答
- 0 关注
- 174 浏览
添加回答
举报
0/150
提交
取消