简体   繁体   中英

Why uploading a file to FTP server in C# add a header and footer to the file

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:

  1. Create a new blank Unity project
  2. Add an empty GameObject in the scene, and Add a new script
  3. Paste the code below and replace the class name with your script file name
  4. Fill attributes with and exemple of file and FTP server
  5. 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

Problem solved: this question is a duplicate of this one Extra data when uploading txt file to FTP server using C#

I used FtpWebRequest instead. The code is much more complicated but it works:

    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
    );

Copy and paste EndGetStreamCallback and FtpState from MSDN documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM