I am uploading a file(Zip with the size 100MB) using WebClient but it works fine some times, Fail some times
Here is My Code Please let me know your Views and What need to be done to Make it Consistent
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 Answer