How to auto start download file on page load?

Source: Internet
Author: User

Hello

I have tried into 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.