Click here to Skip to main content
14,599,199 members
Rate this:
Please Sign up or sign in to vote.
Hi;
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.
Posted

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

First, you need to check up the result returned by fd.ShowDialog(); it might be cancelled. Also, you do the asynchronous operation. The call is not blocking the calling thread by the time the file is actually uploaded. Instead, the upload just starts and will be finished at some later time. You can check up if the file is actually uploaded later.

Also, to synchronize the asynchronous operation, you should use the returned result from WebClient.UploadFileTaskAsync:
http://msdn.microsoft.com/en-us/library/hh194308%28v=vs.110%29.aspx[^].

To understand it and the usage of Task<T> with asynchronous operations, you may need to learn working with Tasks in general.

See also: http://msdn.microsoft.com/en-us/library/dd321424%28v=vs.110%29.aspx[^],
http://msdn.microsoft.com/en-us/library/dd537609(v=vs.110).aspx[^],
http://msdn.microsoft.com/en-us/library/dd460717(v=vs.110).aspx[^].

—SA
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100