C# Webclient Stream download file from FTP to local storage
Have you tried the following usage of WebClient
class?
using (WebClient webClient = new WebClient())
{
webClient.DownloadFile("url", "filePath");
}
Update
using (var client = new WebClient())
using (var stream = client.OpenRead("..."))
using (var file = File.Create("..."))
{
stream.CopyTo(file);
}
If you want to download file explicitly using customized buffer size:
public static void DownloadFile(Uri address, string filePath)
{
using (var client = new WebClient())
using (var stream = client.OpenRead(address))
using (var file = File.Create(filePath))
{
var buffer = new byte[4096];
int bytesReceived;
while ((bytesReceived = stream.Read(buffer, 0, buffer.Length)) != 0)
{
file.Write(buffer, 0, bytesReceived);
}
}
}
OnResolve
INJenuity INJenuity is a framework written in and for Csharp assemblies with the purpose of solving dependency injection, object creation / lifetime management, and writing tedious boiler-plate code. The software engineer and the framework work together by attribute declaration, programmatically-defined configurations, and smart assembly scanning. Through each of these techniques, INJenuity will create code at runtime and inject the created code to the engineer's running assembly. http://nuget.org/packages/INJenuity/ http://tylerhoersch.com/injenuity MockJockey MockJockey is a framework written in and for Csharp assemblies with the purpose of allowing engineers to unit test their code in a precise, concise, and quick manner. Using mock objects in unit tests, allows your unit tests to be focused by side-stepping any irrelevancies of the test. Commonly thought as a necessity for doing test driven development, a mock framework will give your tests exactly what they need to be--small and fast! And that's what MockJockey provides us. http://nuget.org/packages/MockJockey/ http://tylerhoersch.com/mockJockey
Updated on June 17, 2022Comments
-
OnResolve almost 2 years
I've been downloading files from an FTP server via the
WebClient
object that the.NET
namespace provides and then write the bytes to a actual file via aBinaryWriter
. All is good. However, now, the files have dramatically increased in size and I'm worried about memory constraints so I'd like to create a download stream, create an file stream, and line by line read from the download and write to the file.I'm nervous since I couldn't find a nice example of this. Here's my end result:
var request = new WebClient(); // Omitted code to add credentials, etc.. var downloadStream = new StreamReader(request.OpenRead(ftpFilePathUri.ToString())); using (var writeStream = File.Open(toLocation, FileMode.CreateNew)) { using (var writer = new StreamWriter(writeStream)) { while (!downloadStream.EndOfStream) { writer.Write(downloadStream.ReadLine()); } } }
Am I going about this incorrect/better way/etc?
-
OnResolve over 11 yearsYes, and still am (the request object is a WebClient [I'm update my post to show this explictly]) but DownladFile will give me the entire file in memory--the opposite of what I'm looking for..
-
Sergey Vyacheslavovich Brunov over 11 years@OnResolve, sorry, I've not mentioned. Please see the update.
-
Sergey Vyacheslavovich Brunov over 11 years@OnResolve, I've added the version with the customized buffer size.