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.