2012-12-17 58 views
0

您好,我试图通过c#上传一个文件到我的web服务器上,当我上传文件时出现问题我的应用程序冻结,直到文件完成上传,我想要上传异步但我似乎无法让代码在这里工作,有代码和我不断收到的错误。将上传文件转换为uploadfileasync c#

此代码有效,但会冻结表单。

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); 

但是,如果我这样做的代码,我会得到一个错误。

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); 

和当试图使它异步

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' 

回答

6

更改线路

wc.UploadFileAsync(Server, Filename); 

wc.UploadFileAsync(new Uri(Server), Filename); 

UploadFileAsync并不需要一个字符串参数我得到这个错误,所以你需要t o从服务器地址创建一个Uri。有关更多详细信息,请参阅MSDN Docs

+0

谢谢!非常!! – Terrii

3

正如卡米所说。此外,您可能需要处理UploadFileCompleted事件。

例子:

wc.UploadFileCompleted += (o, args) => 
{ 
    //Handle completition 
}; 
wc.UploadFileAsync(new Uri(Server), Filename);