0

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

elpha01
  • 196
  • 2
  • 4
  • 19
  • 1
    You have to show us the code you're using to upload these files. – greyxit Aug 09 '18 at 08:57
  • Sorry, I totally forget, I will edit. – elpha01 Aug 09 '18 at 09:01
  • [mcve], please. 1) Show us your URL, or at least general format of it. 2) I'm pretty sure the event handlers are not needed to reproduce the problem. 3) If synchronous upload still has the problem (pretty sure it does), use synchronous upload. – Martin Prikryl Aug 09 '18 at 11:23
  • 1
    Also, did you try to upload the same file to the same server using the same credentials using standalone FTP client. What does the uploaded file look like then? – Martin Prikryl Aug 09 '18 at 11:25
  • 1) ftp:'//ftp.blabla.com/dir/dir/dir/filename.fileextension (One ' was added to prevent auto-format) 2) No they are not needed 3) Async and Sync produce both the same result 4) I have no problem when manually uploading with WinSCP – elpha01 Aug 10 '18 at 12:11
  • Please edit your question, do not post important information in comments. + Include WinSCP session log file. + You didn't confirm, if you use WinSCP on the same machine as your code. + Include .NET log file too: https://stackoverflow.com/q/9664650/850848 – Martin Prikryl Aug 10 '18 at 12:47
  • Hello, I am sorry for the delay. I will add the WinSCP session log to my original post. I am using WinSCP and .NET on the same machine. I can't print trace log as in the link you provided because this is a Unity project and not a .NET Framework project. – elpha01 Aug 13 '18 at 13:16
  • So create [mcve] for your problem using .NET framework (a simple console application). Btw, aren't you connecting via a proxy with your C# application? – Martin Prikryl Aug 19 '18 at 06:34

1 Answers1

0

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.

elpha01
  • 196
  • 2
  • 4
  • 19