private async Task MakeDownload(Model.Download download, Stream data, byte[] byteBuffer, CancellationToken cancellationToken, FileRenameStreamer fileRenameStreamer) { using (var output = fileRenameStreamer.GetFileStream()) { var onProgressChanged = new Progress <MyProgress>(OnDownloadProgressChanged); var oldPercentDone = 0.0; var bytesRead = 0; download.StartDownloadPart(); do { bytesRead = await data.ReadAsync(byteBuffer, 0, byteBuffer.Length, cancellationToken); download.AddBytesToPart(bytesRead); fileRenameStreamer.AddCurrentBytesRead(bytesRead); if (bytesRead <= 0) { continue; } await output.WriteAsync(byteBuffer, 0, bytesRead, cancellationToken); oldPercentDone = SendDownloadProgress(onProgressChanged, download, oldPercentDone); } while (bytesRead > 0); download.FinishDownloadPart(); download.DumpLogFromAllParts(); CompleteDownload(download); } }
public void PauseDownload(Model.Download download) { if (_webClient.IsBusy) { _webClient.CancelAsync(); } }
private void DeleteDownload(Model.Download download) { download.ResetDownloadPart(); download.TotalFileSize = 0; DownloadCancel?.Invoke(this, new MyDownloadEventArgs()); }
private async void DownloadButton_Click(object sender, RoutedEventArgs e) { Button bt = sender as Button; Contentlist cl = bt.DataContext as Contentlist; sqliteHelper.CreateDB(); List <Model.Download> DL = sqliteHelper.CheckData(cl.name); bool checkresult = false; foreach (Model.Download i in DL) { if (i.name == cl.name) { checkresult = true; } } if (checkresult) { var dialog = new MessageDialog("已经添加过下载了", "yoho"); dialog.Commands.Add(new UICommand("确定", cmd => { })); await dialog.ShowAsync(); return; } Model.Download AddDownload = new Model.Download(); AddDownload.name = cl.name; AddDownload.love = cl.love; AddDownload.hate = cl.hate; AddDownload.profile_image = cl.profile_image; AddDownload.text = cl.text; AddDownload.video_uri = cl.video_uri; AddDownload.create_time = cl.create_time; sqliteHelper.AddData(AddDownload); StorageFile File = await NetWork.DownLoad.DownLoadVideo(cl.video_uri); }
private async void Button_Click(object sender, RoutedEventArgs e) { try { Button button = sender as Button; Model.Download Vediodata = button.DataContext as Model.Download; sqliteHelper.DeleteData(Vediodata); string sql = "delete from sqlite_sequence where name='Download';"; sqliteHelper.UpadateData(sql); DownloadList.Remove(Vediodata); if (DownloadList.Count == 0) { TB.Opacity = 1; } else { TB.Opacity = 0; } string FileName = Vediodata.video_uri; string[] str = FileName.Split('/'); string filename = str[str.Length - 1]; IStorageFolder Folder = ApplicationData.Current.LocalCacheFolder; IStorageFile File = await Folder.GetFileAsync(filename); await File.DeleteAsync(); } catch (Exception) { var dialog = new MessageDialog("删除时出错", "yoho"); dialog.Commands.Add(new UICommand("确定", cmd => { })); await dialog.ShowAsync(); } }
public async void ResumeDownload(Model.Download download) { download.FileRenameCancelToken = new FileRenameCancelToken(); var fileResume = new FileResumeStreamer(download.TempFileName, download.TargetPathWithFileName, download.FileRenameCancelToken); await RunDownload( download, request => request.AddRange(download.GetBytesFromAllParts()), fileResume); }
public async void DownloadFile(Model.Download download) { download.FileRenameCancelToken = new FileRenameCancelToken(); var fileRenameStreamer = new NewFileRenameStreamer(download.TargetPathWithFileName, download.FileRenameCancelToken); download.TempFileName = fileRenameStreamer.GetNewTempFileWithPath(); await RunDownload( download, request => request.AddRange(0), fileRenameStreamer); }
private async void ListView_ItemClick(object sender, ItemClickEventArgs e) { try { Frame fr = this.Parent as Frame; Grid gr1 = e.ClickedItem as Grid; Model.Download cl = gr1.DataContext as Model.Download; Uri = cl.video_uri; string[] str = cl.video_uri.Split('/'); string filename = str[str.Length - 1]; string Path = @"C:\Users\Jin\AppData\Local\Packages\39360532-96e5-4fa1-98dd-49dc39195039_bphk9g7bbxc1j\LocalCache\" + filename; Uri uri = new Uri(Path); if (Grid.GetColumnSpan(fr) == 2) { if (this.Frame != null) { fr.Navigate(typeof(SecondFrame)); SecondFrame sf1 = fr.Content as SecondFrame; Grid gr2 = sf1.Content as Grid; Grid gr3 = gr2.Children[1] as Grid; MediaElement Player = gr3.Children[0] as MediaElement; Player.Source = uri; } return; } else { Grid gr2 = fr.Parent as Grid; Grid gr3 = gr2.Parent as Grid; MainPage mainpage = gr3.Parent as MainPage; Grid gr4 = mainpage.Content as Grid; Grid gr5 = gr4.Children[0] as Grid; Frame fr1 = gr5.Children[0] as Frame; if (this.Frame != null) { fr1.Navigate(typeof(SecondFrame)); } SecondFrame fr2 = fr1.Content as SecondFrame; Grid gr6 = fr2.Content as Grid; Grid gr7 = gr6.Children[1] as Grid; MediaElement player1 = gr7.Children[0] as MediaElement; player1.Source = uri; } } catch (Exception) { var dialog = new MessageDialog("打开失败", "yoho"); dialog.Commands.Add(new UICommand("确定", cmd => { })); dialog.Commands.Add(new UICommand("重新下载", new UICommandInvokedHandler(this.CommandInvokedHandler))); await dialog.ShowAsync(); } }
private void getData(int id) { JavaDLL dll = new JavaDLL(); Model.Download ver = dll.getDownloadList(id); if (ver != null) { txtVername.Text = ver.version; txtVerurl.Text = ver.url; txtVerdate.Text = ver.cdate.ToString("yyyy-MM-dd"); } }
private async Task RunDownload(Model.Download download, Action <HttpWebRequest> configureRequest, FileRenameStreamer fileRenameStreamer) { try { _cancellationTokenSource = new CancellationTokenSource(); var request = (HttpWebRequest)WebRequest.Create(new Uri(download.SourcePath)); configureRequest(request); using (var response = await request.GetResponseAsync()) { fileRenameStreamer.SetContentLength(response.ContentLength); var data = response.GetResponseStream(); using (fileRenameStreamer) { var byteBuffer = fileRenameStreamer.GetByteBuffer(); fileRenameStreamer.SetStreamPosition(download.GetBytesFromAllParts()); download.TotalFileSize = fileRenameStreamer.GetTotalFileSize(); await MakeDownload(download, data, byteBuffer, _cancellationTokenSource.Token, fileRenameStreamer); } } } catch (IOException) { DownloadCancel?.Invoke(this, new MyDownloadEventArgs()); } catch (OperationCanceledException) { if (download.State == CurrentDownloadState.Cancel) { DeleteDownload(download); } else if (download.State == CurrentDownloadState.Pause) { DownloadPause?.Invoke(this, new MyDownloadEventArgs()); } else { DeleteDownload(download); } } catch (WebException) { DownloadCancel?.Invoke(this, new MyDownloadEventArgs()); } }
public async void DownloadFile(Model.Download download) { try { using (_webClient = new WebClient()) { _webClient.DownloadFileCompleted += WebClientOnDownloadFileCompleted; _webClient.DownloadProgressChanged += WebClientOnDownloadProgressChanged; await _webClient.DownloadFileTaskAsync(new Uri(download.SourcePath), download.TargetPathWithFileName); } } catch (Exception e) { DownloadCancel?.Invoke(this, new MyDownloadEventArgs()); } }
public void PauseDownload(Model.Download download) { download.FileRenameCancelToken.Cancel(); _cancellationTokenSource.Cancel(); }
private static double SendDownloadProgress(IProgress <MyProgress> onProgressChanged, Model.Download download, double oldPercentDone) { var myProgress = new MyProgress() { CurrentFileSize = download.GetBytesFromAllParts(), TotalFileSize = download.TotalFileSize }; var percentDone = (double)myProgress.CurrentFileSize / myProgress.TotalFileSize; if (!(percentDone - oldPercentDone > 0.05)) { return(oldPercentDone); } onProgressChanged?.Report(myProgress); return(percentDone); }
private void CompleteDownload(Model.Download download) { DownloadComplete?.Invoke(this, new MyDownloadEventArgs()); download.FileRenameCancelToken = null; }
public void ResumeDownload(Model.Download download) { DownloadFile(download); }