2

I am using Website panel on an IIS server . I want to upload a file using ftp

I used this code :

try
{
     WebClient webClient = new WebClient();

     OpenFileDialog fd = new OpenFileDialog();
     fd.ShowDialog();

     MessageBox.Show(fd.FileName);

     webClient.UploadFileTaskAsync(new Uri("ftp://" + "username" + ":" +"pass" +"@" + "address/" + "name.ext"), fd.FileName);

 }
 catch (Exception ex)
 { 
     MessageBox.Show(ex.Message);
 }

This code runs without error . when I use file manager of website panel , A file created "Name.ext" but this file size is 0 KB. Why? What is my problem ?!

I am sure that windows firewall allow me to do it.

update : I am doing it with UploadFileTask and UploadFile (an unasync) but result is same as async :(

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
Sinaw
  • 61
  • 2
  • 9
  • Look at my code, check the response variable and what its returning. It might contain the ErrorCode you're looking for – Yuval Itzchakov Jul 20 '14 at 19:09
  • Similar question,but not async method. http://stackoverflow.com/questions/15268760/upload-file-to-ftp-using-c-sharp/15270543#15270543 – cdev Jul 21 '14 at 17:32

3 Answers3

3

Your exception might be swallowed inside the unawaited Task which is returning from WebClient.UploadFileTaskAsync. You should await on your task, that way your catch block will detect a potentional exception as it will propogate to the awaited line of code:

public async Task UploadFileByFtpAsync()
{
   try
   {
       WebClient webClient = new WebClient();

       OpenFileDialog fd = new OpenFileDialog();
       fd.ShowDialog();

       MessageBox.Show(fd.FileName);

       Task<byte[]> response = await webClient.UploadFileTaskAsync(new Uri("ftp://" + "username" + ":" +"pass" +"@" + "address/" + "name.ext"), fd.FileName);

       // You might want to validate the response status code is valid here
    }
    catch (Exception ex)
    { 
        MessageBox.Show(ex.Message);
        // Do useful exception handling here
    }
}

Edit

As i said in the comment, check the response variable, it contains the response of your upload attempt which might have failed.

Edit 2

It seems the issue is with the KeepAlive property of the web/ftp request. There is an accepted answer in The underlying connection was closed: The server committed a protocol violation. FTP and here

Community
  • 1
  • 1
Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • 1
    Yeah , I think you are right . This time it give a error "The uderlying connection was closed : The server committed a protocol violation ." and create file ... Is it a server error ?! can I myself solve it ?! – Sinaw Jul 20 '14 at 19:26
2

My first thought is that you're not awaiting the Async call and thus whatever process or thread that's running this code might be ending before the UploadFileTaskAsync call is actually finished.

Try changing the last call of your try block to

await webClient.UploadFileTaskAsync(new Uri("ftp://" + "username" + ":" +"pass" +"@" + "address/" + "name.ext"), fd.FileName);

You probably also want to check the return value of the call:

var result = await webClient.UploadFileTaskAsync(new Uri("ftp://" + "username" + ":" +"pass" +"@" + "address/" + "name.ext"), fd.FileName);

The result object is a byte[] of the response from the server.

Tim
  • 14,999
  • 1
  • 45
  • 68
  • I thought it at first and wait a lot of time for it . And change it to UploadFile (An Unasync) but the result is same as UploadFileTaskAsync :( – Sinaw Jul 20 '14 at 19:03
1

I recommend to use a native FtpWebRequest Class to upload a file.

     try
     {
        OpenFileDialog fd = new OpenFileDialog();
        fd.ShowDialog();
        MessageBox.Show(fd.FileName);

        FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp://address/" + fd.FileName);
        request.Method = WebRequestMethods.Ftp.UploadFile;
        request.Credentials = new NetworkCredential("username", "pass");
        request.UsePassive = true;
        request.UseBinary = true;
        request.KeepAlive = false;

        FileStream stream = File.OpenRead(fd.FileName);
        byte[] buffer = new byte[stream.Length];

        stream.Read(buffer, 0, buffer.Length);
        stream.Close();

        Stream reqStream = request.GetRequestStream();
        reqStream.Write(buffer, 0, buffer.Length);
        reqStream.Close();
       }
    catch (Exception ex)
    { MessageBox.Show(ex.Message);


    }
user3806621
  • 278
  • 1
  • 6