Пример #1
0
        // Inicia a leitura dos dados para escrever em disco...
        private void WSR_BeginReadStream(IAsyncResult WSR_ReadStream)
        {
            FileGetterStreamRequest fgsr = (FileGetterStreamRequest)WSR_ReadStream.AsyncState;
            int bytesRead = fgsr.webStreamRead.EndRead(WSR_ReadStream);

            // Escreve a quantidade de bytes lidos no arquivo
            fgsr.fileStreamWrite.Write(this.bTmpRead, 0, bytesRead);
            fgsr.fileStreamWrite.Flush();

            // Marca o total que foi escrito do arquivo em disco
            this.fileDownloadedSize += bytesRead;

            // Limpa o array lido desde o inicio
            Array.Clear(this.bTmpRead, 0, this.bTmpRead.Length);

            // Define o tamanho total do arquivo baixado
            // Caso seja completado o download
            if (this.fileDownloadedSize >= this.fileTotalSize)
            {
                fgsr.webStreamRead.Close();
                fgsr.webResponse.Close();
                fgsr.fileStreamWrite.Close();

                this.OnEventHandlerCall(FileGetterEnum.REQUEST_DOWNLOAD_FINISHED, string.Empty, this.fileTotalSize, this.fileDownloadedSize);
                return;
            }

            // Informa que o arquivo está sendo baixado...
            this.OnEventHandlerCall(FileGetterEnum.REQUEST_DOWNLOADING, string.Empty, this.fileTotalSize, this.fileDownloadedSize);

            // Solicita uma nova leitura de dados referente ao arquivo que está sendo baixado.
            fgsr.webStreamRead.BeginRead(this.bTmpRead, 0, this.bTmpRead.Length, this.WSR_BeginReadStream, fgsr);
        }
Пример #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="WR_Response"></param>
        private void WR_beginGetResponse(IAsyncResult WR_Response)
        {
            FileGetterStreamRequest fgsr = new FileGetterStreamRequest();

            fgsr.httpWebRequest = (HttpWebRequest)WR_Response.AsyncState;

            try
            {
                fgsr.webResponse = fgsr.httpWebRequest.EndGetResponse(WR_Response);

                // Tamanho total do arquivo a ser baixado
                this.fileTotalSize = fgsr.webResponse.ContentLength;

                // Define o tamanho total do arquivo baixado
                // Caso seja completado o download
                if (this.fileDownloadedSize >= this.fileTotalSize)
                {
                    fgsr.webResponse.Close();

                    this.OnEventHandlerCall(FileGetterEnum.REQUEST_DOWNLOAD_FINISHED, string.Empty, this.fileTotalSize, this.fileDownloadedSize);
                    return;
                }

                // Informa que stá calculando o tamanho do arquivo para iniciar o download...
                this.OnEventHandlerCall(FileGetterEnum.REQUEST_COMPUTING_SIZE, string.Empty, this.fileTotalSize, 0);

                // Verifica é possível continuar um download pausado, se não for possível, então
                // Zera o tamanho da requisição
                bool bSupportContinue = String.Compare(fgsr.webResponse.Headers["Accept-Ranges"], "bytes") == 0;

                // Zera o que foi obtido do download do arquivo
                if (!bSupportContinue)
                {
                    File.Delete(this.pathFile);
                    this.fileDownloadedSize = 0;
                }

                // Informa que o download está sendo iniciado e também a posição inicial do download...
                this.OnEventHandlerCall(FileGetterEnum.REQUEST_STARTING_DOWNLOAD, string.Empty, this.fileTotalSize, this.fileDownloadedSize);

                // Obtém o stream de conexão com o arquivo que sera baixado.
                fgsr.webStreamRead = fgsr.webResponse.GetResponseStream();
                // Obtém o stream de escrita do arquivo.
                fgsr.fileStreamWrite = new FileStream(this.pathFile, FileMode.OpenOrCreate);

                // Inicializa os bytes para leitura...
                this.bTmpRead = new byte[512];

                // Inicia a leitura dos dados
                fgsr.webStreamRead.BeginRead(this.bTmpRead, 0, this.bTmpRead.Length, this.WSR_BeginReadStream, fgsr);
            }
            catch
            {
                File.Delete(this.pathFile);
                fgsr.httpWebRequest.AddRange(0);
                fgsr.httpWebRequest.BeginGetResponse(this.WR_beginGetResponse, fgsr.httpWebRequest);
            }
        }