Exemple #1
0
        private void _webClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            if (!_updating)
            {
                return;
            }

            FileDownloadProgress?.Invoke(e.ProgressPercentage);
        }
Exemple #2
0
        private void Connection_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
        {
            FileDownloadInfo info = e.UserState as FileDownloadInfo;

            if (info == null)
            {
                return;
            }

            info.Args.Progress = e.ProgressPercentage * 0.01;

            FileDownloadProgress?.Invoke(this, info.Args);
        }
Exemple #3
0
        private void Connection_UploadValuesCompleted(object sender, UploadValuesCompletedEventArgs e)
        {
            FileDownloadInfo info = e.UserState as FileDownloadInfo;

            if (info == null)
            {
                return;
            }

            if (e.Error != null || e.Cancelled)
            {
                info.Args.Progress = -1;
                FileDownloadError?.Invoke(this, info.Args);
            }

            info.Args.Progress = 1;
            FileDownloadProgress?.Invoke(this, info.Args);

            lock (info.LocalFile.Directory)
            {
                if (!info.LocalFile.Directory.Exists)
                {
                    info.LocalFile.Directory.Create();
                }
            }

            var           fs = info.LocalFile.OpenWrite();
            var           ms = new MemoryStream(e.Result);
            DeflateStream df = new DeflateStream(ms, CompressionMode.Decompress);

            df.CopyTo(fs);
            df.Close();
            fs.Close();

            FileDownloadCompleted?.Invoke(this, info.Args);

            StartDLJob(info);
        }
Exemple #4
0
 /// <summary>
 /// Event für das Fortschritt des Datei-Downloads auslösen
 /// </summary>
 /// <param name="position"></param>
 protected virtual void OnFileDownloadProgress(long position)
 {
     FileDownloadProgress?.Invoke(this, new FileDownloadProgressEventArgs(position));
 }
        public async static Task <bool> DownloadFile(string fileName, int id, string authToken)
        {
            string tempfile = Path.Combine(Constants.API_FILE_DOWNLOAD_FOLDER, fileName);

            var request = new HttpRequestMessage(HttpMethod.Get, Constants.API_FILEHOST_GET_FILE);

            request.Headers.Add("File-Name", fileName);
            request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", authToken);


            HttpResponseMessage response = await ApiClient.SendAsync(request, HttpCompletionOption.ResponseHeadersRead);


            if (response.StatusCode != HttpStatusCode.OK)
            {
                // Something wrong
                FileDownloadResultEventArgs result_args = new FileDownloadResultEventArgs();
                result_args.Successful = false;
                result_args.FileName   = fileName;
                result_args.Reason     = response.ReasonPhrase;
                FileDownloadFailure?.Invoke(null, result_args);
                return(false);
            }

            var totalBytes = 0L;

            if (response.Headers.Contains("File-Size"))
            {
                totalBytes = Convert.ToInt64(response.Headers.GetValues("File-Size").FirstOrDefault());
            }

            Stream contentStream = await response.Content.ReadAsStreamAsync();

            FileStream fileStream = new FileStream(tempfile, FileMode.Create, FileAccess.Write, FileShare.None, 8192, true);


            var totalRead    = 0L;
            var totalReads   = 0L;
            var buffer       = new byte[8192];
            var isMoreToRead = true;

            do
            {
                var read = await contentStream.ReadAsync(buffer, 0, buffer.Length);

                if (read == 0)
                {
                    isMoreToRead = false;
                }
                else
                {
                    await fileStream.WriteAsync(buffer, 0, read);

                    totalRead  += read;
                    totalReads += 1;

                    if (totalReads % 2000 == 0)
                    {
                        FileTransferProgressEventArgs progress_args = new FileTransferProgressEventArgs();
                        progress_args.FileName = fileName;
                        progress_args.ID       = id;
                        decimal pp = (decimal)totalRead / (decimal)totalBytes;

                        progress_args.PercentProgress = (int)Math.Round(pp * 100);

                        FileDownloadProgress?.Invoke(null, progress_args);


                        //Console.WriteLine(string.Format("total bytes downloaded so far: {0:n0}", totalRead));
                    }
                }
            }while (isMoreToRead);



            FileDownloadResultEventArgs args = new FileDownloadResultEventArgs();

            args.Successful = true;
            args.FileName   = fileName;
            args.ID         = id;

            FileDownloadSuccessful?.Invoke(null, args);

            return(true);
        }