Monday, June 29, 2009

C#: Write a file from an URL response

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();
}

1 comment:

  1. 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

Please use your common sense before making a comment, and I truly appreciate your constructive criticisms.