public void onRemotingCallback(string callUID, string methodName, object result, AsyncOption option) { switch (methodName) { case "MailManager.syncUserMail": { int is_synced_value = -1; ASObject mail = option.asyncData as ASObject; int is_synced = NumberUtil.parseInt(mail["is_synced"] == null ? "0" : mail["is_synced"].ToString()); if (is_synced == 0) { is_synced = 1; is_synced_value = 1; } if (is_synced == 1) { //同步文件 string mail_file = mail["mail_file"] as string; string store_path = System.IO.Path.Combine(new string[] { Desktop.instance.ApplicationPath, "mail" }); string mail_file_path = store_path + mail_file; if (File.Exists(mail_file_path)) { string uuid = mail["uuid"] as string; Dictionary<string, string> param = new Dictionary<string, string>(); param["uuid"] = MailReceiveWorker.getFilePath(uuid); using (var client = new CookieAwareWebClient()) { client.Param = param; string uri = Desktop.getAbsoluteUrl(upload_mail_message); mail_file_path = mail_file_path.Replace("/", "\\"); client.UploadFileAsync(new Uri(uri), mail_file_path); } is_synced_value = 2; } } if (is_synced_value != -1) { mail["is_synced"] = is_synced_value; updateMailRecord(mail, new string[] { "is_synced" }); } } break; } }
internal string UploadFileToVF(string zipFile, string swiftFilename, UploadPercent callback) { Login(); string fileUploadInfo = SendGetRequest(ServerUrl + "/testbench/client_upload_url?filename=" + HttpUtility.UrlEncode(swiftFilename)); if (fileUploadInfo == null) { // job creation failed //job.Status = Job.StatusEnum.Failed; return null; } int maxProgress = 0; object maxProgressLock = new object(); dynamic fileUploadInfoJson = Newtonsoft.Json.Linq.JObject.Parse(fileUploadInfo); string fileUploadUrl = fileUploadInfoJson["url"].Value; try { try { using (CookieAwareWebClient webClient = new CookieAwareWebClient()) { webClient.cookies = this.AuthCookies; webClient.Timeout = HTTP_WEB_TIMEOUT_BASE; Semaphore progress = new Semaphore(0, int.MaxValue); Semaphore completed = new Semaphore(0, 1); UploadFileCompletedEventArgs completedData = null; webClient.UploadFileCompleted += delegate(object sender, UploadFileCompletedEventArgs e) { completedData = e; completed.Release(); }; webClient.UploadProgressChanged += delegate(object sender, UploadProgressChangedEventArgs e) { lock (maxProgressLock) { int percentProgress = (int)(((double)e.BytesSent / e.TotalBytesToSend) * 99.0); if (percentProgress > maxProgress) { // e.ProgressPercentage is not correct, as (almost) all the time is spent uploading maxProgress = percentProgress; callback(maxProgress); } } progress.Release(); }; webClient.UploadFileAsync(new Uri(fileUploadUrl), "PUT", zipFile); while (true) { WaitHandle[] handles = new WaitHandle[] { completed, progress }; int timeout = HTTP_WEB_TIMEOUT_BASE; lock (maxProgressLock) { // Workaround for Fiddler proxy: Fiddler accepts all the data at once, then uploads to VF if (maxProgress > 98) { handles = new WaitHandle[] { completed }; timeout = HTTP_WEB_REQUEST_TIMEOUT; } } int handle = WaitHandle.WaitAny(handles, timeout); if (handle == 0) { lock (maxProgressLock) { maxProgress = 100; callback(maxProgress); } break; } if (handle == System.Threading.WaitHandle.WaitTimeout) { webClient.CancelAsync(); throw new WebException("Connection timed out"); } if (handle != 1) throw new Exception(); } Trace.TraceInformation("Upload to S3 " + Encoding.UTF8.GetString(completedData.Result)); return fileUploadUrl; } } catch (System.Reflection.TargetInvocationException ex) { throw ex.InnerException; } } catch (WebException ex) { Trace.TraceError("Error uploading to {0}. Exception follows", fileUploadUrl); Trace.TraceError(ex.ToString()); } return null; }