C
C#16mo ago
kolya112

❔ Problem with WebClient

Downloading a file using DownloadFile(). What can limit file upload size via WebClient? There are files with a size of 1.4 and 1.5 GB (they have the same size on the server), they are loaded correctly in the browser, but from under the code, via WebClient.DownloadFile() - no, only +- 1.05 GB is loaded
13 Replies
Thinker
Thinker16mo ago
Is there a reason you're using WebClient?
kolya112
kolya11216mo ago
No, I have been using it for a long time, although I know that it is outdated. What can you recommend for a replacement?
Thinker
Thinker16mo ago
HttpClient Essentially you just do this
HttpClient client = new();
var response = await client.GetAsync(url);
var file = await Response.Content.ReadAsByteArrayAsync();
HttpClient client = new();
var response = await client.GetAsync(url);
var file = await Response.Content.ReadAsByteArrayAsync();
kolya112
kolya11216mo ago
I have the names of the files to download (one by one) are taken from the array, the values ​​of which are sorted out using foreach. Wouldn't async interfere with this?
Thinker
Thinker16mo ago
If you await everything properly then it shouldn't be an issue Anywhere something returns a Task, you await it.
kolya112
kolya11216mo ago
Thx
kolya112
kolya11216mo ago
The same thing happened. Initially, everything was fine, when suddenly the process of downloading both files stopped at the same one as on WebClient: 1.05 GB instead of 1.4 and 1.5 GB. I had a try catch block and threw an exception "System.IO.IOException" in System.dll - "An error occurred while copying the content to the stream"
Thinker
Thinker16mo ago
hmm
Tvde1
Tvde116mo ago
sounds like something is wrong with the web server
kolya112
kolya11216mo ago
The browser downloads them perfectly. All 1.5 and 1.4 GB. And C# only +- 1.05 GB and then gives an error.
Anton
Anton16mo ago
Are you sure this doesn't load the whole thing into memory first?
mindhardt
mindhardt16mo ago
Consider doing HttpClient.GetAsStreamAsync Not sure if it helps tho
Accord
Accord16mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.