Reputation: 385
Hello I'm trying to upload a file to my web server via c# and I'm having a problem when uploading the file my application freezes until file is done uploading and I wanted to upload it Async but I cannot seem to get the code to work here is there code and the error I keep getting.
This code works but freezes the form.
WebClient wc = new WebClient();
wc.Credentials = new System.Net.NetworkCredential(TxtUsername.Text, TxtPassword.Text);
string Filename = TxtFilename.Text;
string Server = TxtServer.Text + SafeFileName.Text;
wc.UploadFile(Server, Filename);
But if i do this code I get an error.
WebClient wc = new WebClient();
wc.Credentials = new System.Net.NetworkCredential(TxtUsername.Text, TxtPassword.Text);
string Filename = TxtFilename.Text;
string Server = TxtServer.Text + SafeFileName.Text;
wc.UploadFileAsync(Server, Filename);
and I get this error when trying to make it Async
Error 1 The best overloaded method match for System.Net.WebClient.UploadFileAsync(System.Uri, string)' has some invalid arguments.
Error 2 Argument 1: cannot convert from 'string' to 'System.Uri'
Upvotes: 0
Views: 937
Reputation: 8469
As Kami said. Furthermore, you might want to handle the UploadFileCompleted
event.
Example:
wc.UploadFileCompleted += (o, args) =>
{
//Handle completition
};
wc.UploadFileAsync(new Uri(Server), Filename);
Upvotes: 3