Esempio n. 1
0
        public void Download()
        {
            DownloadContext downloadcontext = new DownloadContext(Path.Combine(_directory, tempFileName));

            try
            {
                using (var clien = new HttpClient())
                {
                    if (downloadcontext.StartBytes > 0)
                    {
                        clien.DefaultRequestHeaders.Range = new RangeHeaderValue(downloadcontext.StartBytes, null);
                    }
                    using (var response = clien.GetAsync(this._url, HttpCompletionOption.ResponseHeadersRead, CancellationToken.None).Result)
                    {
                        StartDownload(response, downloadcontext);

                        _fileName = downloadcontext.GetFileName(response.Content.Headers);
                    }
                }
                FinishedDownload(downloadcontext);
            }
            catch (Exception e)
            {
                var msg = e.Message;
            }
            finally
            {
                if (downloadcontext.CurrentStream != null)
                {
                    downloadcontext.CurrentStream.Dispose();
                }
            }
        }
Esempio n. 2
0
 private void FinishedDownload(DownloadContext context)
 {
     context.Dispose();
     FileReName();
     if (fileloaded != null)
     {
         fileloaded.Invoke();
     }
 }
Esempio n. 3
0
        private void StartDownload(HttpResponseMessage response, DownloadContext downloadcontext)
        {
            using (var stream = response.Content.ReadAsStreamAsync().Result)
            {
                byte[] bufferArrray = new byte[_bufferSize];

                downloadcontext.SetFileSize(response.Content.Headers);

                int readSize = stream.Read(bufferArrray, 0, _bufferSize);

                while (readSize > 0)
                {
                    downloadcontext.StartBytes += readSize;
                    downloadcontext.CurrentStream.Write(bufferArrray, 0, readSize);

                    readSize = stream.Read(bufferArrray, 0, _bufferSize);
                    fileloading.Invoke(downloadcontext.StartBytes, (long)downloadcontext.FileSize);
                }
            }
        }