问题描述
winform客户端向网站上传文件,开始使用普通的WebClient上传文件,进度条是正常的,但这种方式的确定是客户端会假死,所以改成了WebClient的异步上传方式,这种方式不会假死了,但进度条始终不走动了,始终是0的位置,文件是上传成功了的,望高手帮我看下。privatevoidbutton1_Click(objectsender,EventArgse){//前面的数据库查询省略,会得到pic_List.Count与m_fileList.Length这两个的数据值。progressBar1.Minimum=0;progressBar1.Maximum=pic_List.Count+m_fileList.Length;progressBar1.Value=0;stringm_url=“http://www.cccc.com/ssss.ashx”foreach(stringiteminpic_List){if(File.Exists(AppDomain.CurrentDomain.BaseDirectory+item)){//开始上传图片附件currRest+=UploadAtta(m_url,AppDomain.CurrentDomain.BaseDirectory+item)+"n";}progressBar1.Value++;}foreach(stringiteminm_fileList){if(File.Exists(item)){//开始上传文档附件currRest+=UploadAtta(m_url,item)+"n";}progressBar1.Value++;}MessageBox.Show("文档上传结束,以下文档已经上传:n"+currRest);}//上传文件到服务器privatestaticstringUploadAtta(stringm_url,stringFileName){stringstrurl=m_url+"?path="+FileName.Substring(0,FileName.LastIndexOf("\"));stringcurrFileName=FileName.Substring(FileName.LastIndexOf("\")+1,FileName.Length-FileName.LastIndexOf("\")-1);try{//异步上传WebClientmyClient=newWebClient();if(myClient.IsBusy){myClient.CancelAsync();}myClient.UploadFileAsync(newUri(strurl),"POST",FileName);myClient.UploadFileCompleted+=newUploadFileCompletedEventHandler(myClient_UploadFileCompleted);//myClient.CancelAsync();return"【"+currFileName+"】";}catch(Exceptionex){return"【"+currFileName+"】失败!";}}privatestaticvoidmyClient_UploadFileCompleted(objectsender,UploadFileCompletedEventArgse){if(e.Error==null&&e.Cancelled==false){//MessageBox.Show("文档上传失败!");}}
解决方案
本帖最后由 shifan360 于 2015-06-23 13:26:29 编辑
解决方案二:
http://blog.csdn.net/shihuan10430049/article/details/3734398参考
解决方案三:
用BackgroundWorker来做会直观一些。
解决方案四:
lz的处理方式不行的,主线程被阻塞了进度计算使用单独线程,刷新界面采用委托方式