[英]Inconsistent in uploading a file using webclient (C#)
我正在使用WebClient上传文件(大小为100MB的ZIP文件),但有时工作正常,有时失败
这是我的代码,请让我知道您的观点以及需要做些什么来使其保持一致
public bool UploadFirmware(string srcPath, string destPath)
{
bool status = false;
List<Unisys.sPar.FirmwareInfo> orgFwList = new List<Unisys.sPar.FirmwareInfo>();
List<Unisys.sPar.FirmwareInfo> upFwList = new List<Unisys.sPar.FirmwareInfo>();
try
{
orgFwList = EnumerateFirmware();
Uri fwuri = new Uri(destPath);
string myStringWebResource = null;
WebClient myWebClient = new WebClient();
myStringWebResource = fwuri.ToString();
myWebClient.Encoding = Encoding.UTF8;
myWebClient.Credentials = new NetworkCredential(userName, password);
myWebClient.Headers[HttpRequestHeader.ContentType] = "application/zip";
bool gExecuteOnce = true;
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
myWebClient.UploadFileCompleted += new UploadFileCompletedEventHandler(myWebClientUploadloadFileCompleted);
myWebClient.UploadFileAsync(new System.Uri(myStringWebResource), "POST", srcPath);//upload method
while (myWebClient.IsBusy)
{
System.Threading.Thread.Sleep(10000);
if (gExecuteOnce == true)
{
SPARTestToolInit.logger.Log(NLog.LogLevel.Info, "Please Wait Uploading firmware.....");
gExecuteOnce = false;
}
}
upFwList = EnumerateFirmware();
if (upFwList.Count == orgFwList.Count + 1)
{
status = true;
SPARTestToolInit.logger.Log(NLog.LogLevel.Info, "Firmware Uploaded sucessfuly .....");
}
}
catch (Exception ex)
{
status = false;
SPARTestToolInit.logger.Log(NLog.LogLevel.Error, "Unable to upload firmware due to " + ex.ToString());
}
return status;
}
1. don't pool if webclient busy, you have a callback myWebClientUploadloadFileCompleted
2. set successfull status in you completed callback not in your method
static ManualResetEvent done = new ManualResetEvent(false);
public bool UploadFirmware(string srcPath, string destPath)
{
bool status = false;
List<Unisys.sPar.FirmwareInfo> orgFwList = new List<Unisys.sPar.FirmwareInfo>();
List<Unisys.sPar.FirmwareInfo> upFwList = new List<Unisys.sPar.FirmwareInfo>();
try
{
orgFwList = EnumerateFirmware();
Uri fwuri = new Uri(destPath);
string myStringWebResource = null;
WebClient myWebClient = new WebClient();
myStringWebResource = fwuri.ToString();
myWebClient.Encoding = Encoding.UTF8;
myWebClient.Credentials = new NetworkCredential(userName, password);
myWebClient.Headers[HttpRequestHeader.ContentType] = "application/zip";
bool gExecuteOnce = true;
ServicePointManager.ServerCertificateValidationCallback = new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);
myWebClient.UploadFileCompleted += new UploadFileCompletedEventHandler(myWebClientUploadloadFileCompleted);
myWebClient.UploadFileAsync(new System.Uri(myStringWebResource), "POST", srcPath);//upload method
done.WaitOne();//wait signal from completed callback
}
catch (Exception ex)
{
status = false;
SPARTestToolInit.logger.Log(NLog.LogLevel.Error, "Unable to upload firmware due to " + ex.ToString());
}
return status;
}
static void client_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
{
done.Set();
}
static void client_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
//set your progress log
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.