Created
September 9, 2010 16:40
-
-
Save codereflection/572134 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Castle.MonoRail.Framework; | |
using System.Xml.Serialization; | |
namespace Ris.Operations.Oats.Presentation.Attributes | |
{ | |
[AttributeUsage(AttributeTargets.ReturnValue, AllowMultiple = false, Inherited = false)] | |
public class XlsReturnBinderAttribute : Attribute, IReturnBinder | |
{ | |
private IEngineContext engineContext; | |
private IControllerContext controllerContext; | |
private Type returnType; | |
private object returnValue; | |
public void Bind(IEngineContext engineContext, IController controller, | |
IControllerContext controllerContext, Type returnType, object returnValue) | |
{ | |
this.returnValue = returnValue; | |
this.returnType = returnType; | |
this.controllerContext = controllerContext; | |
this.engineContext = engineContext; | |
ProcessResponse(); | |
} | |
private void ProcessResponse() | |
{ | |
ModifyControllerContext(); | |
RenderXls(); | |
CancelRenderView(); | |
SetContentType(); | |
} | |
private void RenderXls() | |
{ | |
var serializer = new XmlSerializer(returnType); | |
serializer.Serialize(engineContext.Response.OutputStream, returnValue); | |
} | |
private void ModifyControllerContext() | |
{ | |
controllerContext.LayoutNames = null; | |
} | |
private void CancelRenderView() | |
{ | |
controllerContext.SelectedViewName = null; | |
} | |
private void SetContentType() | |
{ | |
engineContext.Response.ContentType = "application/xls"; | |
engineContext.Response.AppendHeader("Content-Disposition", "inline; filename=\"download.xls\""); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment