Click here to Skip to main content
15,886,519 members
Articles / Programming Languages / Javascript

File Download Using JavaScript

Rate me:
Please Sign up or sign in to vote.
4.39/5 (12 votes)
29 Jan 2010CPL2 min read 252.5K   6.3K   47  
An article on how to download a file using JavaScript
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace FileDownloadUsingJavascript
{
    public partial class FileHandler : System.Web.UI.Page
    {
        private const string APP_SCRIPTS = "appLoaderScripts";
        protected void Page_Load(object sender, EventArgs e)
        {
            Type type = this.GetType();
            if (!this.ClientScript.IsClientScriptIncludeRegistered(type, "scrScripts"))
                this.ClientScript.RegisterClientScriptInclude(type, "scrScripts", "Common/Loaders/LoadScripts.aspx");
            if (!this.ClientScript.IsStartupScriptRegistered(type, "scrHandlerStartUp"))
                this.ClientScript.RegisterStartupScript(type, "scrHandlerStartUp", GetStartUpScript(), true);
        }

        private string GetStartUpScript()
        {
            return "try{{var handler = new FileHandler();handler.init();}}catch(e){{document.getElementById('divNotLoaded').style.display = '';}}";
        }
        
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Public License Version 1.0 (CPL)


Written By
Software Developer
United States United States
I started development with Visual Basic 6.0 and then upgraded to Visual Studio .Net and working on .net for 7+ years now.

Comments and Discussions