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之旅

posted @ 2018-12-19 21:59  自由的鱼  阅读(145)  评论(0编辑  收藏  举报