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
to
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();
}
to
int MaxInt = int.MaxValue;
using (BinaryReader br = new BinaryReader(stream))
{
content = br.ReadBytes(MaxInt);
br.Close();
}
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();
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Honestly, I have just two lines
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.
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)));
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.
ASKER
Thanx
I'm building both source and destination paths elsewhere
So whats the difference between the 2 lines below?
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);
}
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.
ASKER
Thanx
ASKER
Can you show me?