0

Update: This question is not a duplicate with the How do I authenticate a WebClient request?

i am using cefsharp.

can someone guide me how to download a file using cefsharp.

i tried doing some research, but i don't know, or i don't understand how to use the codes.

what i am doing is download files using cefsharp. the program need to login.

in order to download the files. i am done with the login part. i already have the links.

but i don't know how to add functionality to download some files programatically.

so far i have this code to download file

private string getFilename(string hreflink)
        {
            Uri uri = new Uri(hreflink);

            string filename = System.IO.Path.GetFileName(uri.LocalPath);

            return filename;
        }

        private void downloadFile(string url)
        {
            try
            {

                //string url = "https://cdn.spacetelescope.org/archives/images/large/heic1509a.jpg";
                string filename = getFilename(url);

                using (WebClient wc = new WebClient())
                {

                    wc.DownloadProgressChanged += wc_DownloadProgressChanged;
                    wc.DownloadFileCompleted += wc_DownloadFileCompleted;
                    wc.DownloadFileAsync(new Uri(url), System.IO.Path.GetTempPath() + filename);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                MessageBox.Show(ex.StackTrace);
            }
        }
        private void wc_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            // In case you don't have a progressBar Log the value instead 
            // Console.WriteLine(e.ProgressPercentage);
            Console.WriteLine(e.ProgressPercentage);
        }

        private void wc_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            //progressBar1.Value = 0;

            if (e.Cancelled)
            {
                MessageBox.Show("The download has been cancelled");
                return;
            }

            if (e.Error != null) // We have an error! Retry a few times, then abort.
            {
                MessageBox.Show("An error ocurred while trying to download file");
                return;
            }

            MessageBox.Show("File succesfully downloaded");
        }

Actually the code above works if the file to be downloaded doesn't require username or password.

i even tried

wc.UseDefaultCredentials = true;
                    wc.Credentials = new NetworkCredential(txtUsername.Text, txtPassword.Text);

but it doesn't download the file. so i'm thinking that i can make this work if the cefsharp i used to login will download the file.

can someone guide a beginner like me? thank you

Update: I have this code now. does my code download the files already? if yes how can i save the downloaded file? thank you

browser.Load(Url);
    browser.DownloadHandler = new MyDownloadHandler();

class MyDownloadHandler : IDownloadHandler
        {
            public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
            {
                Console.WriteLine("before");
                Console.WriteLine(downloadItem.FullPath);
                Console.WriteLine(downloadItem.SuggestedFileName);

            }

            public void OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
            {
                if (downloadItem.PercentComplete == 100)
                {
                    Console.WriteLine("Completed");
                }
                Console.WriteLine("update:"+ downloadItem.PercentComplete);

                Console.WriteLine(downloadItem.CurrentSpeed);
            }
        }

Update 3: the download is working now but it only download the first link

how can i download the other link. or how can i make the program finish the first link before going to the next link?

foreach (var item in collection)
    {
browser.Load(Url);
        browser.DownloadHandler = new MyDownloadHandler();
    }

using my code above it only download the first link.

Community
  • 1
  • 1
pdf to image
  • 369
  • 6
  • 23
  • Search the source for StartDownload, you'll also need to implement the IDownloadHandler interface. Read the XML doc for a description of each method and search the source for an example. – amaitland Feb 10 '17 at 07:56
  • http://cefsharp.github.io/api/55.0.0/html/R_Project_Documentation.htm – amaitland Feb 10 '17 at 08:00
  • @amaitland please see my updated code above, and can you tell me if im doing it right? thank you – pdf to image Feb 10 '17 at 08:17
  • WebClient isn't broken. You don't need CefSharp or a browser to download anything. Username/password credentials though work only if the server uses basic authentication. If it used form authentication, you need to get a cookie from the login page first, then try to download. Remove `wc.UseDefaultCredentials = true;` too, that means "Use the Windows account" – Panagiotis Kanavos Feb 10 '17 at 08:51
  • Only assign download handler once. You can use StartDownload to initiate a download without user interaction – amaitland Feb 10 '17 at 10:39
  • The duplicate status should be removed, topic title asks how to download using CefSharp, so seems like a valid question – amaitland Feb 10 '17 at 10:39
  • @amaitland how can i make the cefsharp download without user interaction? or no save file dialog popup? thank you – pdf to image Feb 13 '17 at 02:54
  • Implement IDialogHandler – amaitland Feb 13 '17 at 03:20

0 Answers0