PDA

View Full Version : Download File using DirectEvent



Daniil
Dec 13, 2013, 4:02 AM
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 (https://github.com/extnet/Ext.NET/issues/210) 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 (http://examples2.ext.net/#/GridPanel/Miscellaneous/Export_Data_Ajax/). 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 (http://forums.ext.net/showthread.php?19430&p=83757&viewfull=1#post83757) which might be useful in some scenarios.

As an alternative of downloading via AJAX, you can consider downloading via Flash, for example.