I am trying to upload files to a FTP server in C#. The upload works properly beside the fact that all my files, once on the server, have a header and a footer. Here an exemple:
--------------8d5fde16cb03f95
Content-Disposition: form-data; name="file"; filename="PNPP190618.manifest"
Content-Type: application/octet-stream
<Real file content here>
--------------8d5fde16cb03f95
Of course I checked that my original files don't have this text and I uploaded some binary files that have the same problem.
Thank you.
EDIT: Forget to show you my code:
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 session log (no problem with this method): https://pastebin.com/sv7FNC0i
EDIT3: I am not using a proxy.
I tried to reproduce the problem on a minimal exemple on both Unity 2017 (Mono .NET 3.5) and a Console project (.NET Framework 3.5). There is no problem with .NET Framework but it does in Unity 2017.
Here are the steps to reproduce a minimal Unity project:
- Create a new blank Unity project
- Add an empty GameObject in the scene, and Add a new script
- Paste the code below and replace the class name with your script file name
- Fill attributes with and exemple of file and FTP server
Press Play
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 () { } }
EDIT4: I just found a duplicate that I missed before : Extra data when uploading txt file to FTP server using C#
I will try the solution mentionned