Dec 13, 2013, 4:02 AM
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
WebForms: download with an HTTP handler
Page
View
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.
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 handlerusing 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;
}
}
}
}
MVCView
<!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>
Controllerusing 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 handlersUnfortunately, 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.