Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Downloading event for use in progress bar #62

Closed
ehinolakingsley opened this issue Oct 2, 2017 · 8 comments
Closed

Downloading event for use in progress bar #62

ehinolakingsley opened this issue Oct 2, 2017 · 8 comments

Comments

@ehinolakingsley
Copy link

Please can u kindly including event for downloading status. I will like to using it for download progress. I will also like to get a constant byte downloaded event. Thanks

@greg-db
Copy link
Contributor

greg-db commented Oct 2, 2017

Thanks for the post! I'm sending this along as a feature request.

As a workaround though, you can implement progress for downloads manually as shown here:

https://www.dropboxforum.com/t5/API-support/How-to-get-Progress-Status-while-uploading-downloading-a-file-in/m-p/170685/highlight/true#M6405

@ehinolakingsley
Copy link
Author

I am soooooooooooo much grateful. Thanks for you extreme quick response. I got your message.

@ehinolakingsley
Copy link
Author

Working perfectly.....

@ehinolakingsley
Copy link
Author

hi.
While working with the above code, I figured out that if their is any break in connection and it reconnect after few secs. The download will pass 100%.

@greg-db
Copy link
Contributor

greg-db commented Oct 4, 2017

Thanks for pointing that out! That's just a basic example without any real error handling. If you use that when writing your app, make sure you connect it to your actual app logic and add whatever error handling is necessary.

@ehinolakingsley
Copy link
Author

Thanks. Was able to resolve it by adding a condition inside the while loop to check for internet connection and access.

@ehinolakingsley
Copy link
Author

async void RunDownload()
{
using (var dbx = new DropboxClient(AutoUpdate.DropBoxID))
{
var response = await dbx.Files.DownloadAsync(ApplicationName);
ulong fileSize = response.Response.Size;
const int bufferSize = 1024 * 1024;

            var buffer = new byte[bufferSize];

            using (var stream = await response.GetContentAsStreamAsync())
            {
                using (var file = new FileStream(DownloadPath, FileMode.OpenOrCreate))
                {
                    var length = stream.Read(buffer, 0, bufferSize);

                    while (length > 0)
                    {
                        try
                        {
                            if (isDownloadCancelled == true)
                            {
                                buffer = null;
                                fileSize = 0;
                                cts.Cancel();
                                dbx.Dispose();
                                stream.Dispose();
                                file.Flush();
                                file.Dispose();
                                GC.Collect();
                                GC.WaitForPendingFinalizers();
                                break;
                            }

                            var state =iInternetAvailiable.IsInternetAvailable;

                            if (state == true)
                            { 
                                    file.Write(buffer, 0, length);

                                    var percentage = 100 * (ulong)file.Length / fileSize;


                                    Dispatcher.Invoke(new Action(() =>
                                    {
                                        DownloadingStatus.Text = string.Format("Download Status: Downloaded {0}...{1}% ", iDirectoryMemorySizes.MemorySizeTranslator(file.Length), percentage);
                                        progressBarCurrent.Value = (int)percentage;

                                        if (percentage == 100)
                                        {
                                            DownloadingStatus.Text = "Download Status: Completed";
                                            btnInstall.IsEnabled = true;
                                            progressBarCurrent.Foreground = Brushes.LightGreen;

                                            isDownloadCancelled = true;
                                        }

                                    }), DispatcherPriority.ContextIdle);

                                    length = stream.Read(buffer, 0, bufferSize);

                                }                                 

                            else if(state== false)
                            {
                                Dispatcher.Invoke(new Action(() =>
                                {
                                    iMessageBox.iShow("An error has occurred. Internet connection lost!!!", "Timeout", iButtons.Ok, iMessageType.Error_Message_RedColor);
                                    this.Close();

                                isDownloadCancelled = true;
                                }), DispatcherPriority.ContextIdle);
                            } 

                        }
                        catch (Exception)
                        { }
                        
                    }
                    
                }
            }

        }
    }

@ehinolakingsley
Copy link
Author

Thanks a lot. You really saved my struggle to a week now. Thanks onces again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants