Link to home
Start Free TrialLog in
Avatar of trevor1940
trevor1940

asked on

C#: How to save files from a URL with unknown size possibly using CefSharp

Hi
I've recently been introduced to CefSharp

Given a uri and a local file path  how do I save a file?

It  makes sense to use  CefSharp to  save the file  has  it should handle any user credentials
In the examples there is a DownloadHandler.cs I've not found any tutorials on how to use it

I've been using the code bellow in other applications form here but that requires you to know how big the file is prior to download which isin't often the case

Alternatively how do I amend this so it's not governed  by a random byte size
admittedly I could change

 using (BinaryReader br = new BinaryReader(stream))
            {
                content = br.ReadBytes(5000000);
                br.Close();
            }
           

Open in new window

 

to

            int MaxInt = int.MaxValue;
            using (BinaryReader br = new BinaryReader(stream))
            {
                content = br.ReadBytes(MaxInt);
                br.Close();
            }

Open in new window

but that seems ridicules not knowing if a 100kb jpg is attempting to be saved to a 2GB + file
 
        private static void save_file_from_url(string url, string file_name)
        {
            byte[] content;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            WebResponse response = request.GetResponse();



            Stream stream = response.GetResponseStream();

            using (BinaryReader br = new BinaryReader(stream))
            {
                content = br.ReadBytes(5000000);
                br.Close();
            }
            response.Close();

            FileStream fs = new FileStream(file_name, FileMode.Create);
            BinaryWriter bw = new BinaryWriter(fs);
            try
            {
                bw.Write(content);
            }
            finally
            {
                fs.Close();
                bw.Close();
            }
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chinmay Patel
Chinmay Patel
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of trevor1940
trevor1940

ASKER

I use webclient exclusively for most of my download scenarios

Can you show me?
Honestly, I have just two lines

WebClient client = new WebClient();
                        client.DownloadFileAsync(new Uri(this.downloadQueue[i].OriginalUrl), string.Concat(targetDirectory, "\\", this.downloadQueue[i].Title, ".", Path.GetExtension(this.downloadQueue[i].OriginalUrl)));

Open in new window


The major part of the code is just building the both source and destination paths.

This code downloads thousands of files from Flickr - some of them go up to 1 GB.
Thanx

I'm building both source and destination paths elsewhere  

So whats the difference between the 2 lines below?

        private void save_file_from_url_EE(string href, string localFile)
        {
            WebClient client = new WebClient();
            client.DownloadFileAsync(new Uri( href), localFile);
            client.DownloadFile(new Uri(href), localFile);
        }

Open in new window

The async call as the name suggests will execute asynchronously. Keeping your app responsive and downloading the file in background while your app performs other functions.
Thanx