internal async Task UpdateAsync() { App.Window.Dispatcher.Invoke(() => { MainWindow.DownloadDialog.ShowAsync(); MainWindow.DownloadDialog.DownloadUpdateAsync(this); }); OnDownloadProgressChanged?.Invoke(0); WebClient webClient = new WebClient(); webClient.DownloadProgressChanged += (sender, e) => { OnDownloadProgressChanged?.Invoke(e.ProgressPercentage); }; string tempFname = Path.GetTempFileName(); await webClient.DownloadFileTaskAsync(UpdateUrl, tempFname); OnDownloaded?.Invoke(); Thread.Sleep(3000); string oldFilename = Assembly.GetExecutingAssembly().Location; string ps = Resources.UpdateScript.Replace("##01", tempFname).Replace("##02", oldFilename); ExecuteCommand(ps); Environment.Exit(0); }
public async Task <ObjectResult <object> > DownloadPackage(int packageId, string token) { Uri url = new Uri(ApiUrl + $"download?token={token}&package_id={packageId}"); OnDownloadProgressChanged?.Invoke(0); WebClient webClient = new WebClient(); webClient.DownloadProgressChanged += (sender, e) => { OnDownloadProgressChanged?.Invoke(e.ProgressPercentage); }; string tempFname = Path.GetTempFileName(); await webClient.DownloadFileTaskAsync(url, tempFname); if (ZipTools.IsCompressedData(tempFname)) { return(new ObjectResult <object>(1, "Package succesfully downloaded!", tempFname)); } else { ObjectResult <object> obj = JsonConvert.DeserializeObject <ObjectResult <object> >(File.ReadAllText(tempFname)); obj.code = 0; obj.content = tempFname; return(obj); } }
public IEnumerator DownloadingFiles(List <UrlFilePath> urlFilePaths, ICancellationToken cancellationToken, OnDownloadCompleted onCompleted, OnDownloadProgressChanged onProgressChanged) { bool noError = true; for (int i = 0; i < urlFilePaths.Count; i++) { if (cancellationToken.Canceled) { noError = false; break; } int currentI = i; int currentCount = urlFilePaths.Count; UrlFilePath item = urlFilePaths[i]; yield return(GameLogicUpdateSystem.StartCoroutine(DownloadingFile(item.Url, item.FilePath, cancellationToken, (ne) => { if (!ne) { noError = ne; } }, (percents) => { int currentPercents = Mathf.RoundToInt((100 * currentI + percents) / (float)currentCount); onProgressChanged?.Invoke(currentPercents); }))); } onCompleted?.Invoke(noError); }
/// <summary> /// 下载文件 /// </summary> /// <returns></returns> public void DownFile() { Task.Run(() => { if (_updateModel == null) { OnError?.Invoke("请先执行比较版本步骤"); OnStep?.Invoke(UpdateStepEnum.Fail); return; } OnStep?.Invoke(UpdateStepEnum.DownFile); // 升级下载文件地址校验 if (string.IsNullOrWhiteSpace(UpdateJsonAddress) || !_updateModel.DownFileAddress.MatchUrl()) { OnError?.Invoke($"下载升级文件地址校验失败:{_updateModel.DownFileAddress}"); OnStep?.Invoke(UpdateStepEnum.Fail); return; } _updateModel.DownFileSavePath = $"{DownFileSavePath}\\{Path.GetFileName(_updateModel.DownFileAddress)}"; // 创建目录 if (!Directory.Exists(DownFileSavePath)) { Directory.CreateDirectory(DownFileSavePath); } // 验证证书 ServicePointManager.ServerCertificateValidationCallback += (o, c, ch, er) => true; // 下载 var webClient = new WebClient(); webClient.Headers.Add("User-Agent", "Mozilla / 5.0(compatible; MSIE 9.0; Windows NT 6.1; Trident / 5.0)"); webClient.DownloadProgressChanged += (sender, e) => { OnDownloadProgressChanged?.Invoke(e); }; webClient.DownloadFileCompleted += (sender, e) => { if (e.Error != null) { OnError?.Invoke($"下载升级文件失败:{e.Error.Message} 地址:{_updateModel.DownFileAddress}"); OnStep?.Invoke(UpdateStepEnum.Fail); return; } OnDownloadFileCompleted?.Invoke(); if (IsSilent && IsExtract) { // 解压文件 ExtractFile(); } else { // 删除文件 DeleteFile(); OnStep?.Invoke(UpdateStepEnum.Complete); } }; // 开始下载 webClient.DownloadFileAsync(new Uri(_updateModel.DownFileAddress), _updateModel.DownFileSavePath); }); }
private IEnumerator DownloadingFile(string url, string filePath, ICancellationToken cancellationToken, OnDownloadCompleted onCompleted, OnDownloadProgressChanged onProgressChanged) { bool anyError = true; using (UnityWebRequest unityWebRequest = new UnityWebRequest(url, UnityWebRequest.kHttpVerbGET)) { unityWebRequest.downloadHandler = new DownloadHandlerFile(filePath) { removeFileOnAbort = true }; UnityWebRequestAsyncOperation asyncOperation = unityWebRequest.SendWebRequest(); int lastPercents = 0; while (!asyncOperation.isDone) { yield return(new WaitForEndOfFrame()); if (cancellationToken.Canceled) { unityWebRequest.Abort(); break; } int currentPercents = Mathf.RoundToInt(asyncOperation.progress * 100f); if (lastPercents < currentPercents) { onProgressChanged?.Invoke(currentPercents); lastPercents = currentPercents; } } anyError = unityWebRequest.isNetworkError || unityWebRequest.isHttpError; if (anyError) { Debug.LogError(unityWebRequest.error); } else { Debug.Log($"{nameof(HttpClient)}: File successfully downloaded and saved to " + filePath); } } onCompleted?.Invoke(!anyError); }
internal void TriggerDownloadProgressChangedEvent(DownloadDetails details, long downloadedSize, long totalSize) { OnDownloadProgressChanged?.Invoke(this, details, downloadedSize, totalSize); }
private void onDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e) { OnDownloadProgressChanged?.Invoke(sender, e); }