How to download multiple files parallel in C#.NET Console

How to download multiple files parallel in ASP.NET MVc

This article will show you how to download multiple files in ASP.NET MVC, We are using Parallel.ForEach loop to download multiple file.

Parallel.ForEach executes each element parallel and wait till they finish. You can specify how many elements may execute in parallel using “MaxDegreeOfParallelism”

Here is simple example

var arrayStrings = new string[1000];
Parallel.ForEach<string>(arrayStrings, someString =>
{
    //DoSomething(someString);
});

Download multiple files

  class Program
    {
        static void Main(string[] args)
        {
            List<Document> documents = new List<Document>();
            //// ... initialize document List ...

            var options = new ParallelOptions { MaxDegreeOfParallelism = 2 };
            var tasks = Parallel.ForEach(documents, options,
            s =>
            {
                lock ((Document)s)
                {
                    Document document = (Document)s;
                    if (document != null)
                    {

                        string fileurl =  document.Source;
                        string fileSavePathtemp = document.Destination;
                        HttpWebRequest request;
                        HttpWebResponse response = null;
                        try
                        {
                            request = (HttpWebRequest)WebRequest.Create(fileurl);
                            request.Credentials = System.Net.CredentialCache.DefaultCredentials;
                            request.Timeout = 10000;
                            request.AllowWriteStreamBuffering = false;
                            response = (HttpWebResponse)request.GetResponse();
                            Stream stream = response.GetResponseStream();
                            //Write to disk
                            FileStream fstream = new FileStream(fileSavePathtemp, FileMode.Create);
                            byte[] read = new byte[256];
                            int count = stream.Read(read, 0, read.Length);
                            while (count > 0)
                            {
                                fstream.Write(read, 0, count);
                                count = stream.Read(read, 0, read.Length);
                            }
                            //Close everything
                            fstream.Close();
                            stream.Close();
                            response.Close();
                        }
                        catch (Exception e)
                        {
                        }
                    }
                }
            });
        }
        public class Document
        {
            public string Source { get; set; }
            public string Destination { get; set; }
        }
    }
Advertisements
This entry was posted in C#.NET, MVC and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s