我正在嘗試使用 C# 將文件上傳到 FTP 服務(wù)器。除了我的所有文件(一旦在服務(wù)器上)都有頁眉和頁腳這一事實(shí)之外,上傳工作正常。這里有一個(gè)例子:--------------8d5fde16cb03f95Content-Disposition: form-data; name="file"; filename="PNPP190618.manifest"Content-Type: application/octet-stream<Real file content here>--------------8d5fde16cb03f95當(dāng)然我檢查了我的原始文件沒有這個(gè)文本,我上傳了一些有同樣問題的二進(jìn)制文件。謝謝你。編輯:忘記向您展示我的代碼: 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 會(huì)話日志(此方法沒有問題):https : //pastebin.com/sv7FNC0iEDIT3:我沒有使用代理。我試圖在 Unity 2017 (Mono .NET 3.5) 和控制臺(tái)項(xiàng)目 (.NET Framework 3.5) 的最小示例上重現(xiàn)該問題。.NET Framework 沒有問題,但在 Unity 2017 中有問題。以下是重現(xiàn)最小 Unity 項(xiàng)目的步驟:創(chuàng)建一個(gè)新的空白 Unity 項(xiàng)目在場(chǎng)景中添加一個(gè)空的 GameObject,并添加一個(gè)新腳本粘貼下面的代碼并將類名替換為您的腳本文件名用文件和FTP服務(wù)器的示例填充屬性按播放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貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超10個(gè)贊
我改用了 FtpWebRequest。代碼要復(fù)雜得多,但它有效:
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 文檔中復(fù)制并粘貼 EndGetStreamCallback 和 FtpState。
- 1 回答
- 0 關(guān)注
- 201 瀏覽
添加回答
舉報(bào)
0/150
提交
取消