How to auto start download file on page load?

Hello 

i have tried many methods:

1. normal way: 

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString.Get("formid") != null)
            {
                int.TryParse(Request.QueryString.Get("formid"), out formId);
                hfID.Value = formId.ToString();

                DownloadFile(formId);
            }
        }
    }
this way doesn't work. actually ,the file was downloaded, but the current page wasn't displayed. 

 

2.using Thread.

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (Request.QueryString.Get("formid") != null)
            {
                int.TryParse(Request.QueryString.Get("formid"), out formId);
                hfID.Value = formId.ToString();

                
                ThreadStart starter = delegate { DownloadFile(formId); };
                //ThreadStart 
                Thread oThread = new Thread(starter);
                //oThread.IsBackground = true;
                oThread.Start();
            }
        }
    }
i got an error in method "DownloadFile", maybe in the new thread, some assemblies were not loaded.

 

3.use asp.net event : Page_Unload

    protected void Page_UnLoad(object sender, EventArgs e)
    {
        DownloadFile(formId);
        //base.OnUnload(e);
    }
the downloading is working ,but problem is the current page doesn't displayed.

 

4.use javascript. 

first, we need to create a new linkbutton event:

protected void LinkButton1_Click(object sender, EventArgs e)
    {
        int.TryParse(hfID.Value, out formId);
        DownloadFile(formId);
    }

second:

<script language="javascript">
    $(document).ready(function() {
        __doPostBack('dnn$ctr599$DocumentFormDownload$LinkButton1', '');
    });
</script>
when page was completed load, then the js code will be excueted. 

the way is working on FF,Chrome , but not on IE. 

actually , it's working on IE, but we just got an notification,see below: 

this way is similar with :http://sourceforge.net/projects/emule/files/eMule/0.50a/eMule0.50a-Installer.exe/download

 

do you have good way for auto download the file? 

any ideas will be appreciated.

posted @ 2010-09-10 17:26  无尽思绪  阅读(1039)  评论(1编辑  收藏  举报