如何在page_load返回后保持我的WebClient异步下载

时间:2014-05-08 08:30:04

标签: c# asp.net .net webforms

public class DownloadMyFileAsync{

    private WebClient client;

    public void DownloadFileAsync(){
        client = new WebClient();
        client.DownloadComplete += new AsyncCompletedEventHandler (DownloadFileCallback);
        client.DownloadFileAsync("someurl.jpg","c:/somepic.jpg");
    }
    private void DownloadFileCallback(Object sender, AsyncCompletedEventArgs e){
            //stuff
    }
}

在我的网络表单中:

static DownloadMyFileAsync downloader;
protected void Page_Load(object sender, EventArgs e)
{
     if(downloader == null){
        downloader = new DownloadMyFileAsync;
     }
     downloader.DownloadFileAsync();
}

这有时会下载部分文件,有时甚至没有。当我调试并单步执行代码时,它似乎下载了整个文件。我猜测某些事情已被摧毁"当Page_Load方法结束时清理或清理,因此当Page_Load返回时整个文件停止。我该如何维护下载?

0 个答案:

没有答案