WPF Téléchargement de fichier avec WebClient

Inscrit
2 Juin 2018
Messages
32
Reactions
0
#1
Bonsoir,

Je vous expose mon problème, je n'arrive pas à comprendre pourquoi lorsque je lance et utilise ce petit programme le téléchargement est instantanée. Par ailleurs, le fichier download depuis l'URI est bien présent dans le répertoire mais avec une taille de 0 octets.

Code :
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace UplauncherDofus
{
    public partial class MainWindow : Window
    {
        private WebClient m_client;
        private Uri m_uri = null;
        public static readonly DependencyProperty ProgressDependencyProperty = DependencyProperty.Register("ProgressProperty", typeof(string), typeof(MainWindow), new PropertyMetadata("0 %"));
        public static readonly DependencyProperty BtnDownloadToggleDependencyProperty = DependencyProperty.Register("BtnDownloadToggle", typeof(bool), typeof(MainWindow), new PropertyMetadata(false));
        public string ProgressProperty
        {
            get => this.GetValue(ProgressDependencyProperty).ToString();
            set => this.SetValue(ProgressDependencyProperty, value);
        }
        public bool BtnDownloadToggle
        {
            get => (bool)this.GetValue(BtnDownloadToggleDependencyProperty);
            set => this.SetValue(BtnDownloadToggleDependencyProperty, value);
        }

        public MainWindow()
        {
            InitializeComponent();
        }

        #region Gestionnaire events

        private void TxtBoxUri_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (this.TxtBoxUri.Text == string.Empty || this.TxtBoxUri.Text.Trim() == string.Empty)
                this.BtnDownloadToggle = false;
            else
                this.BtnDownloadToggle = true;
        }
        private  void BtnDownload_Click(object sender, RoutedEventArgs e)
        {
            this.m_uri = new Uri(this.TxtBoxUri.Text);
            this.m_client.DownloadFileAsync(this.m_uri, System.IO.Path.GetFileName($"{Environment.CurrentDirectory}/{this.m_uri.AbsolutePath}"));
          
        }
        private void Uplauncher_Loaded(object sender, RoutedEventArgs e)
        {
            this.m_client = new WebClient();
            this.m_client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(this.ClientDownloadProgressChanged);
            this.m_client.DownloadFileCompleted += new AsyncCompletedEventHandler(this.DownloadFileCompleted);
        }

        private void DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            MessageBox.Show("Download complete !", "Informations", MessageBoxButton.OK, MessageBoxImage.Information);
        }

        private void ClientDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            double receieve = (double)e.BytesReceived;
            double total = (double)e.TotalBytesToReceive;
            int percentage = (int)(Math.Truncate(receieve / total) * 100);
            this.ProgressBar.Value = percentage;
        }

        #endregion Gestionnaire events
    }
}
Screen :

upload_2018-7-21_18-24-42.png upload_2018-7-21_18-24-42.png
 
Inscrit
2 Juin 2018
Messages
32
Reactions
0
#2
Problème résolu, finalement le soucis était à ce niveau : int percentage = (int)(Math.Truncate(receieve / total) * 100);
 
Haut Bas