I have an asp.net MVC site and a controller action that looks like this:
public ActionResult GeneratePPT(MyParams deckParams)
{
string template = Server.MapPath("/PPTTemplate.pptx");
var results = _model.GeneratePPT(template);
return File(results.Content, "application/vnd.ms-powerpoint", results.FileName);
}
The issue is: MyParams object is getting very large (lots of parameters) so I would like to change this from a querystring to an ajax post to avoid long querystring issue (as I'm hitting the limit for Internet Explorer of 2083 characters in the URL.
the problem is that I don't see how I could return a file as part of a JsonResponse so I'm looking for recommendations on how I could both:
I had an idea of doing an ajax post to the server, having the server save a file and just return a path in a jsonResponse. Then have the client hit the server again to get the file. Does this make sense? Is there a more elegant way to do this in one step?
I would create normal form and controller Action that return's FileResult:
@model MyApplication.Models.MyParams
@using (Html.BeginForm("GeneratePPT", "PttDownloader", FormMethod.Post, new { id = "downloadTestForm"}))
{
//form data here
@Html.HiddenFor(model => Model.name)
@Html.HiddenFor(model => Model.age)
<input type="submit"/>
}
Action of normal controller:
using System;
using System.Web.Mvc;
using System.Net.Mime;
namespace MyApplication.Controllers
{
public class PttDownloaderController : Controller
{
public FileResult GeneratePPT(MyParams deckParams)
{
try
{
//do something with deckParams...
//deckParams.name
//deckParams.age
string template = Server.MapPath("/PPTTemplate.pptx");
var results = _model.GeneratePPT(template);// provide _model
return File(results.Content, MediaTypeNames.Application.Octet, results.FileName);
}
catch (Exception)
{
return null;
}
}
}
}
Form can be submitted by typical <input type="submit" /> or if you need to invoke this from Javascript you can use example below, both ways will always return file for download:
var download = function() {
var downloadTestForm = $('#downloadTestForm');
downloadTestForm.submit();
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With