Hi Vlad, thanks for the reply.
This, I don't think is the issue though. I have IsUpload set to true and I get the same behaviour whether on a form or not. I can also carry out file downloads okay (edit: file downloads using action results in other scenarios i should point out).
The problem is the parsing of the data. Normally, when the export of grid data is carried out you have a function like:
switch (format)
{
case "xml":
string strXml = e.Xml.OuterXml;
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xml", "Test"));
Response.AddHeader("Content-Length", strXml.Length.ToString());
Response.ContentType = "application/xml";
Response.Write(strXml);
break;
case "xls":
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", "Test"));
XslCompiledTransform xtExcel = new XslCompiledTransform();
xtExcel.Load(HttpContext.Current.Server.MapPath("~/Content/Excel.xsl"));
xtExcel.Transform(e.Xml, null, Response.OutputStream);
break;
case "csv":
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.csv", "Test"));
XslCompiledTransform xtCsv = new XslCompiledTransform();
xtCsv.Load(HttpContext.Current.Server.MapPath("~/Content/Csv.xsl"));
xtCsv.Transform(e.Xml, null, Response.OutputStream);
break;
}
This works fine with code behind in WinForms and MVC 1 however, in MVC 2 it will not work because the MVC View Engine makes assumptions about the resultant OutputStream. For example see:
http://efreedom.com/Question/1-22611...lAnswersMarker
"When a ViewPage begins executing, it assumes certain things about the remainder of the request. The particular thing that is tripping you up is that a ViewPage assumes that the remainder of the request will be a normal HTML or some other textual response, so it switches the response's TextWriter with its own writer."
Therefore, I have had to write a custom ActionResult like the following:
public class ExportResult : ActionResult
{
private readonly string format;
public ExportResult(string format)
{
this.format = format;
}
public override void ExecuteResult(ControllerContext context)
{
var response = context.RequestContext.HttpContext.Response;
var request = context.RequestContext.HttpContext.Request;
XmlNode submitConfig = JsonConvert.DeserializeXmlNode(request.Params["submitDirectEventConfig"]);
string data = string.Empty;
XmlNode serviceNode = submitConfig.SelectSingleNode("config/serviceParams");
if (serviceNode != null)
{
data = serviceNode.InnerText;
}
data = HttpUtility.HtmlDecode(data);
XmlNode node = JsonConvert.DeserializeXmlNode("{records:{record:" + data + "}}");
response.Clear();
switch (format)
{
case "xml":
string strXml = node.OuterXml;
response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xml", "Test"));
response.AddHeader("Content-Length", strXml.Length.ToString());
response.ContentType = "application/xml";
response.Write(strXml);
break;
case "xls":
response.ContentType = "application/vnd.ms-excel";
response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.xls", "Test"));
XslCompiledTransform xtExcel = new XslCompiledTransform();
xtExcel.Load(HttpContext.Current.Server.MapPath("~/Content/Excel.xsl"));
xtExcel.Transform(node, null, response.OutputStream);
break;
case "csv":
response.ContentType = "application/octet-stream";
response.AddHeader("Content-Disposition", string.Format("attachment; filename={0}.csv", "Test"));
XslCompiledTransform xtCsv = new XslCompiledTransform();
xtCsv.Load(HttpContext.Current.Server.MapPath("~/Content/Csv.xsl"));
xtCsv.Transform(node, null, response.OutputStream);
break;
}
response.Flush();
}
}
However, this is not ideal for two reasons, I have had to take a load of the Ext code which turns the submitDirectEventConfig parameters into an XmlNode, whereas with the code behind Url this is done for you. I may have to stick with this, but wondered if there was a better way?