我正在使用 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;
}