예제 #1
0
        public void executeOperation(Button btn, Label lbl, String url, IDownloadObserver observer)
        {
            mIDownloadObserver = observer;
            //
            if (!System.Net.NetworkInformation.NetworkInterface.GetIsNetworkAvailable())
            {
                if (mIDownloadObserver != null)
                {
                    mIDownloadObserver.onMessage(new DownloadArgument("NetworkFailed"));
                }
                return;
            }

            if (btn.Text == "Cancel")
            {
                btn.Text = "Download";
                cancel(url);
                return;
            }

            FileDownloader fd = create(lbl, url, observer, btn);

            if (fd.DownloadStatus == "Continue")
            {
                return;
            }

            if (fd.DownloadStatus != "Completed")
            {
                fd.DownloadAsync(url, this);
                btn.Text    = "Cancel";
                btn.Enabled = true;
            }
        }
예제 #2
0
        void IDownloadObserver.onComplete(DownloadArgument e)
        {
            if (e.Downloader.Tag != null)
            {
                Button btn = (Button)e.Downloader.Tag;
                if (e.AsyncCompletedEventArgs.Cancelled)
                {
                    btn.Enabled = true;
                    btn.Text    = "Download";
                }
                else if (e.AsyncCompletedEventArgs.Error != null)
                {
                    // Error in Completion...
                }
                else
                {
                    //Normal Complete..We can check Downloded Size and Remote Size to verify...
                    btn.Text    = "Completed";
                    btn.Enabled = false;
                    e.Downloader.DownloadObserver = null;
                    mIDownloadObserver            = null;
                }
            }

            if (mIDownloadObserver != null)
            {
                mIDownloadObserver.onComplete(e);
            }

            //

            //this.Remove(e.Downloader);
            //e.Downloader = null;
        }
예제 #3
0
        //
        public async System.Threading.Tasks.Task DownloadAsync(string remoteUri, IDownloadObserver observer)
        {
            this.url = remoteUri;
            // Set the Notiifcation Observer..
            this.DownloadObserver = observer;
            //
            String fileName = Path.GetFileName(remoteUri);
            string filePath = Directory.GetCurrentDirectory() + "\\" + fileName;

            this.downloadLocation = filePath;

            mHttpClientWithProgress = new HttpClientWithProgress(remoteUri, filePath, this);
            {
                this.downloadStatus = "Continue";
                //
                mHttpClientWithProgress.ProgressChanged += (totalFileSize, totalBytesDownloaded, progressPercentage) => {
                    //Console.WriteLine($"{progressPercentage}% ({totalBytesDownloaded}/{totalFileSize})");
                    this.uodateStatus($"{progressPercentage}% ({totalBytesDownloaded}/{totalFileSize})");
                    if (progressPercentage == 100)
                    {
                        this.onComplete(this, new AsyncCompletedEventArgs(null, false, null));
                    }
                };



                await mHttpClientWithProgress.StartDownload();
            }
        }
예제 #4
0
        public FileDownloader exeute(Label lblProgress, String url, IDownloadObserver IDownloadObserver)
        {
            mIDownloadObserver = IDownloadObserver;
            //
            FileDownloader fd = new FileDownloader(lblProgress);

            this.Add(fd);
            fd.DownloadAsync(url, this);
            return(fd);
        }
예제 #5
0
        // We can pass all the ULRs here..in a list...
        public void executeAllOperation(Button btn, Label lbl, String url, IDownloadObserver observer)
        {
            FileDownloader fd = create(lbl, url, observer, btn);

            if (fd.DownloadStatus == "Continue")
            {
                return;
            }
            executeOperation(btn, lbl, url, observer);
        }
예제 #6
0
        public FileDownloader create(Label lblProgress, String url, IDownloadObserver IDownloadObserver, Button btn)
        {
            mIDownloadObserver = IDownloadObserver;

            FileDownloader fd = this.getbyID(url);

            if (fd == null)
            {
                fd = new FileDownloader(lblProgress);
                this.Add(fd);
            }

            fd.Tag = btn;

            return(fd);
        }
예제 #7
0
        //
        public void Download(string remoteUri, IDownloadObserver observer)
        {
            this.url = remoteUri;
            // Set the Notiifcation Observer..
            this.DownloadObserver = observer;
            //
            String fileName = Path.GetFileName(remoteUri);
            string filePath = Directory.GetCurrentDirectory() + "\\" + fileName;

            this.downloadLocation = filePath;
            //
            //using (WebClient client = new WebClient())
            mWebClient = new WebClient();
            try
            {
                //To FIX this => "The request was aborted: Could not create SSL/TLS secure channel."
                // using System.Net;
                ServicePointManager.Expect100Continue = true;
                ServicePointManager.SecurityProtocol  = SecurityProtocolType.Tls12;
                // Use SecurityProtocolType.Ssl3 if needed for compatibility reasons

                start = DateTime.Now;
                Uri uri = new Uri(remoteUri);
                //password username of your file server eg. ftp username and password
                //client.Credentials = new NetworkCredential("usr", "pass");

                //delegate method, which will be called after file download has been complete.
                mWebClient.DownloadFileCompleted += new AsyncCompletedEventHandler(onComplete);
                //delegate method for progress notification handler.
                mWebClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(onContinue);
                // uri is the remote url where filed needs to be downloaded, and FilePath is the location where file to be saved
                mWebClient.DownloadFileAsync(uri, filePath);
                //
            }
            catch (Exception e)
            {
                onComplete(this, new AsyncCompletedEventArgs(e, false, null));
            }
        }