How to avoid the 0-byte file on Webclient download error

时间:2023-03-09 03:48:30
How to avoid the 0-byte file on Webclient download error
_client.DownloadDataAsync(new Uri(url));
_client.DownloadDataCompleted += (sender, e) =>
{
try
{
if (e.Result != null && e.Result.Length > 0 && e.Error == null && e.Cancelled == false)
using (FileStream fileStream = new FileStream(path + file_name, FileMode.Create))
fileStream.Write(e.Result, 0, e.Result.Length);
}
catch (Exception ex)
{
OpenOperator.Error(string.Format("列表页生成任务异常,url:{0}", url), ex);
}
};

大规模download会涉及到站点并发问题,请参见:

.Net魔法堂:开启IIS的WebGarden、WebFarm和StateServer之旅

How to avoid the 0-byte file on Webclient download error