public void WriteFileContentsFromUrl(string contentUrl, string filePath)
{
WebClient request = new WebClient();
request.UseDefaultCredentials = true;
byte[] fileContent = request.DownloadData(contentUrl);
FileStream fs = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(fileContent);
bw.Close();
}
This snippet is really nice and simple. But the better solution is to use treads and write request yourself. It would make possible to download file from url in many streams at one time and continue download after loosing Internet connection. It's faster and more secured way to download a file.
ReplyDelete