Click here to Skip to main content
15,886,199 members

Unable to read file after download multi connections in C#

Chung Thanh Nguyen asked:

Open original thread
Can anyone tell me download multi connection mechanism of IDM?How it downloads to temporary files and join them.
how to download a file from internet[^] I using this code to create a program to download one file with 8 connection. I create one connection to download some MB (I divide MB for connection by using webRequest.AddRange(start,end)) and save it with 01.file, 02.file, 03.file. After download successfull but when I Join files, I can't watch video file. Please help me :(
Here is my code:
C#
try
 {
            HttpWebRequest webRequest;
            HttpWebResponse webResponse;
            IWebProxy proxy = null;
            webRequest = (HttpWebRequest)WebRequest.Create(url);
            webRequest.AddRange((int)start, (int)end);
            webRequest.Timeout = 240 * 1000;
            webRequest.Proxy = proxy; //SA??? or DefineProxy
            webResponse = (HttpWebResponse)webRequest.GetResponse();
            fs = new FileStream(
                fileName, FileMode.Append, FileAccess.Write);
            long fileLength = webResponse.ContentLength;
            Stream strm = webResponse.GetResponseStream();
            int SeekPosition = (int)start;
            Console.WriteLine(name);
            strm.ReadTimeout = 60 * 1000;
            int arrSize = 18 * 1024 * 1024; //SA???                 
            byte[] barr = new byte[arrSize];
            long bytesCounter = 0;
            while (true)
            {
                int actualBytes = 0;
                try
                {
                    actualBytes = strm.Read(barr, 0, arrSize);
                }
                catch
                {

                }
                if (actualBytes <= 0)
                {
                    if (fileLength == actualBytes)
                        break;
                    else
                    {
                        if (tryCounter == 0)
                        {
                            tryCounter++;
                            dStart = DateTime.Now;
                            continue;
                        }

                        if (dStart.AddMinutes(1) >= DateTime.Now)
                            break;
                    }
                }

                fs.Write(barr, 0, actualBytes);
                bytesCounter += actualBytes;
                Transfered = bytesCounter;
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: {0}. Message: {1}", name, ex.Message);
        }
        finally
        {
            fs.Flush();
            fs.Close();
        }
Tags: Connection

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900