Example #1
0
            //Starts the background thread for file download and reports the update to the delegates created above.
            private void StartAsyncDownloadThread()
            {
                //trying and catching exception for creating download request.
                try
                {
                    //creating a webrequest and defining its method as GET to download file.
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(_url);
                    request.KeepAlive        = true;
                    request.Timeout          = 30000;
                    request.ReadWriteTimeout = 30000;
                    FileStream DownloadFile;
                    request.Method = "GET";
                    //Getting Total Size of the file to download
                    TotalBytesToDownload = request.GetResponse().ContentLength;
                    FileInfo FileToDownload = new FileInfo(_downloadLocation);

                    //Switching cases for resumable and non resumable as per the value passed in the constructor.
                    if (_mode == DownloadMode.NonResumable)
                    {
                        if (File.Exists(_downloadLocation))
                        {
                            if (FileToDownload.Length > 0 && FileToDownload.Length < TotalBytesToDownload)
                            {
                                if (_DownloadAnyway)
                                {
                                    FileToDownload.Delete();
                                }
                                else
                                {
                                    UnityEngine.Debug.Log("File Already Exsists");
                                    AsyncThread.Join();
                                    return;
                                }
                            }
                        }
                    }
                    else if (_mode == DownloadMode.Resumable)
                    {
                        if (File.Exists(_downloadLocation))
                        {
                            if (_DownloadAnyway && FileToDownload.Length < TotalBytesToDownload)
                            {
                                request        = (HttpWebRequest)WebRequest.Create(_url);
                                request.Method = "GET";
                                request.AddRange((int)FileToDownload.Length);
                                UnityEngine.Debug.Log("Request Headers: " + request.Headers.ToString());
                            }
                            else
                            {
                                UnityEngine.Debug.Log("File Already Exsists");
                                if (DownloadCompletedEvent != null)
                                {
                                    DownloadCompletedEvent(new OnDownloadCompletedEvent(null, false));
                                }
                                AsyncThread.Join();
                                return;
                            }
                        }
                    }
                    ///Getting response from the server.
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    //checking if the file exists and it exists and server supports partial content the append the incomming data of else create new file of 0B.
                    if (File.Exists(_downloadLocation))
                    {
                        if (response.StatusCode == HttpStatusCode.PartialContent)
                        {
                            DownloadFile = new FileStream(_downloadLocation, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                        }
                        else
                        {
                            DownloadFile = new FileStream(_downloadLocation, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                            DownloadFile.SetLength(0);
                        }
                    }
                    else
                    {
                        DownloadFile = new FileStream(_downloadLocation, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                    }
                    BytesDownloaded = DownloadFile.Length;

                    TotalBytesThisSession = 0;

                    UnityEngine.Debug.Log("Response Headers: " + response.Headers);
                    UnityEngine.Debug.Log("Response Status: " + response.StatusDescription);
                    Stream ResponseStream = response.GetResponseStream();
                    //Writing Bytes to files
                    BytesRead = ResponseStream.Read(_Buffer, 0, _Buffer.Length);
                    while (BytesRead > 0 && !_Cancelled)
                    {
                        if (!Paused)
                        {
                            _isbusy = true;
                            DownloadFile.Write(_Buffer, 0, BytesRead);
                            BytesDownloaded       += BytesRead;
                            TotalBytesThisSession += BytesRead;
                            BytesRead              = ResponseStream.Read(_Buffer, 0, _Buffer.Length);

                            //Reporting progress if the given event is registered.
                            if (ProgressChangedEvent != null)
                            {
                                ProgressChangedEvent(new OnProgressChangedEvent(BytesDownloaded, TotalBytesToDownload, TotalBytesThisSession, Paused));
                            }
                        }
                    }
                    //Report the download completion at the end of while loop.
                    if (DownloadCompletedEvent != null)
                    {
                        if (!_Cancelled)
                        {
                            DownloadCompletedEvent(new OnDownloadCompletedEvent(null, false));
                        }
                        else
                        {
                            DownloadCompletedEvent(new OnDownloadCompletedEvent(null, true));
                        }
                    }
                    DownloadFile.Flush();
                    DownloadFile.Close();
                    response.Close();
                    AsyncThread.Abort();
                    AsyncThread.Join();
                    _isbusy = false;
                }
                catch (Exception ex)
                {
                    _isbusy = false;
                    if (DownloadCompletedEvent != null)
                    {
                        DownloadCompletedEvent(new OnDownloadCompletedEvent(ex, false));
                    }
                }
            }