Download File using DirectEvent

  1. #1

    Download File using DirectEvent

    To download a file you should configure a DirectEvent with IsUpload="true". Yes, it is confusing that you have to set up IsUpload for download. Just we picked a wrong name. Now we don't want to rename it in v2.x to avoid introducing a breaking change. But we might rename it in the next major release v3. We have an Issue for that.

    Internally, downloading a file requires a form. Don't worry, even if there is no form on the page, a DirectEvent with IsUpload="true" creates it automatically for you.

    As for the server side, in WebForms you should just write a file to a Response in the common ASP.NET ways. In MVC you can use the standard FileResult class.

    Here is some examples.

    WebForms
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <script runat="server">
        protected void Download(object sender, DirectEventArgs e)
        {
            Response.AddHeader("Content-disposition", "attachment; filename=" + "test.txt");
            Response.ContentType = "application/octet-stream";
            Response.Write("I have been downloaded via a DirectEvent!");
            Response.End();
        }
    </script>
    
    <!DOCTYPE html>
    <html>
    <head runat="server">
        <title>Ext.NET v2 Example</title>
    </head>
    <body>
        <ext:ResourceManager runat="server" />
    
        <ext:Button runat="server" Text="Download">
            <DirectEvents>
                <Click OnEvent="Download" IsUpload="true" />
            </DirectEvents>
        </ext:Button>
    </body>
    </html>
    A more complex example you can find in our Examples Explorer. It demonstrates how to export a GridPanel's data in CSV, XML and Excel formats. It submits the GridPanel's data to the server, then send it back to the browser as a file to download.

    WebForms: download with an HTTP handler

    Page
    <%@ Page Language="C#" %>
    
    <%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
    
    <!DOCTYPE html>
    <html>
    <head runat="server">
        <title>Ext.NET v2 Example</title>
    </head>
    <body>
        <ext:ResourceManager runat="server" />
    
        <ext:Button runat="server" Text="Download">
            <DirectEvents>
                <Click Url="DownloadHandler.ashx" IsUpload="true" />
            </DirectEvents>
        </ext:Button>
    </body>
    </html>
    HTTP handler
    using System.Web;
    
    namespace Work2
    {
        public class DownloadHandler : IHttpHandler
        {
            public void ProcessRequest(HttpContext context)
            {
                context.Response.AddHeader("Content-disposition", "attachment; filename=" + "test.txt");
                context.Response.ContentType = "application/octet-stream";
                context.Response.Write("I have been downloaded via a DirectEvent!");
            }
    
            public bool IsReusable
            {
                get
                {
                    return false;
                }
            }
        }
    }
    MVC

    View
    <!DOCTYPE html>
    <html>
    <head>
        <title>Ext.Net.MVC v2 Example</title>  
    </head>
    <body>
        @Html.X().ResourceManager()
    
        @(Html.X().Button()
            .Text("Download")
            .DirectEvents(events =>
                {
                    events.Click.Url = Url.Action("Download");
                    events.Click.IsUpload = true;
                })
        )
    </body>
    </html>
    Controller
    using System.Web.Mvc;
    using Ext.Net.MVC;
    
    namespace Work2MVC.Controllers
    {
        public class RazorController : Controller
        {
            public ActionResult Index()
            {
                return View();
            }
    
            public ActionResult Download()
            {
                return File(System.Text.Encoding.UTF8.GetBytes("I have been downloaded via a DirectEvent!"),
                     "text/plain",
                     "test.txt" );
            }
        }
    }
    Success and Failure handlers

    Unfortunately, an AJAX download request doesn't fire Success and Failure events. It means, for example, that masking is not quite possible, because no way to determine when the mask should be hidden. Though, there is some approach to mask which might be useful in some scenarios.

    As an alternative of downloading via AJAX, you can consider downloading via Flash, for example.
    Last edited by Daniil; Mar 24, 2014 at 1:20 PM.

Similar Threads

  1. [CLOSED] MVC DirectEvent Download File
    By MWM2Dev in forum 2.x Legacy Premium Help
    Replies: 2
    Last Post: May 17, 2013, 11:49 AM
  2. [CLOSED] DirectEvent File Download plus MaskEdit issue
    By digitek in forum 1.x Legacy Premium Help
    Replies: 5
    Last Post: Jun 11, 2012, 2:08 PM
  3. [CLOSED] How to download a file in DirectEvent?
    By vadym.f in forum 1.x Legacy Premium Help
    Replies: 6
    Last Post: Jun 08, 2012, 1:20 PM
  4. [CLOSED] [1.0] DirectEvent File Download
    By Timothy in forum 1.x Legacy Premium Help
    Replies: 6
    Last Post: Jan 21, 2011, 6:04 PM
  5. [CLOSED] [1.0] - MVC - DirectEvent Download File
    By drkoh in forum 1.x Legacy Premium Help
    Replies: 1
    Last Post: Jul 23, 2010, 8:26 PM

Tags for this Thread

Posting Permissions