0

Ok, so I am testing uploading files to an FTP server by attempting to upload a text file with the contents of "HELLO WORLD". I am given a return of" "upload file completed System.Net.WebClient error -> cancelled ->False". The file seems to appear on the server but when I open it, the contents read:

--------------8d30e4d69803578
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: application/octet-stream

HELLO WORLD
--------------8d30e4d69803578

the code I am using is:

string ftpUserName = "ftpUserName";
string ftpPassword = "ftpPassword";
string ftpURL = "ftp://ftpServer.com/text.txt";
string path = "pathToFile/test.txt"
public static void Test()
{
    System.Uri uri = new System.Uri(ftpURL);
    FileInfo file = new FileInfo(path);
    if (!file.Exists) 
    {
        return;
    }
    using(WebClient wc = new WebClient())
    {
        wc.Credentials = new NetworkCredential(ftpUserName,ftpPassword);
        wc.UploadFileCompleted +=  UploadFileCompleted;
        wc.UploadFileAsync(uri,"STOR",path);
    }
}

Any help would be appreciated

edit

I also just tried with a zip file and it is corrupt. Both the .txt and .zip are also far smaller once they reach the server, so I am assuming the upload has failed because of that error

edit 2

solved it using .net2.0's version of the FtpWebRequest

mattS
  • 3
  • 4
  • So the file exists on the server, but you have it has wrong contents and you get an error? Is the file format still .txt? – Ian H. Dec 27 '15 at 08:03
  • yes the file ends up on the server and is still a .txt but the contents seem to be mangled – mattS Dec 27 '15 at 08:14
  • Probably not related, but since you call the _Async_ version of `UploadFile`, you should _await_ that line, because you might dispose of your web client before file is uploaded. – Jony Adamit Dec 27 '15 at 08:26
  • did you tried read a email content (for get HELLO WORLD) ? – Kiquenet Nov 24 '17 at 11:10

1 Answers1

0

I have just verified - other than the async issue I mentioned, there's nothing wrong with your code.

Jony Adamit
  • 3,178
  • 35
  • 44
  • Sorry, my bad, didn't realize `WebClient` has two versions of asynchronous methods. To use the version I was talking about you can call `UploadFileTaskAsync` instead. But this is a huge subject, you should really read about it before getting into it. – Jony Adamit Dec 27 '15 at 08:58
  • So your code seems quite alright. Are you sure you pasted the same code that gives this behavior? – Jony Adamit Dec 27 '15 at 08:58
  • that is the function I am calling to upload but I think I found my problem, I do not have two versions of the asynchronous methods. I am writing this in unity3D and it forces you to use .net 2 which makes me wonder if maybe I am missing something critical and was reading up on a newer way of doing this – mattS Dec 27 '15 at 09:09
  • Your code should be OK nevertheless. You can also try using `FtpWebRequest` directly. It will give you more control over the request. – Jony Adamit Dec 27 '15 at 09:27
  • Aha! using the 2.0 version of FptWebRequest worked perfectly thanks! – mattS Dec 27 '15 at 16:28