3

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:

  1. Get around the Internet Explorer 2083 character limit in URL
  2. Have the ability to return a PowerPoint file

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?

Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
leora
  • 188,729
  • 360
  • 878
  • 1,366
  • Make ajax post call and send JsonObject (with parameter value) – RandomBoy Feb 23 '15 at 23:42
  • Why not do a POST with regular MVC? Instead of a Web Api POST ? Then your params will no longer hit the querystring length limit, you can post all the data you want for params. – Steen Tøttrup Feb 26 '15 at 12:33
  • 1
    *i am hitting the limit for Internet Explorer of 2083 characters in the URL* - I would be looking at addressing the issue of the length of the URL directly, not trying to work around the limits. – Ant P Feb 26 '15 at 12:37
  • @SteenTøttrup - are you suggesting an ajax post? if so, that will definately solve my querystring problem but i can't then figure out how to return a file from that post . . or are you suggesting a non ajax post like this: http://stackoverflow.com/questions/5524045/jquery-non-ajax-post – leora Feb 26 '15 at 13:07
  • @Ant P - see my comment reply to SteenTottrup – leora Feb 26 '15 at 13:09
  • 1
    @RandomBoy - can you provide an example as I don't understand what you mean by send JsonObject with parameter value when trying to return a file from asp.net-mvc controller action – leora Feb 26 '15 at 13:10
  • @leora What is the problem? Is the problem the long querystring? Is it an requirement that it is handled using javascript/ajax? If not I would just do a regular form post. – Steen Tøttrup Feb 27 '15 at 06:38
  • @leora: I had a same problem in two month back. What I did is generate a file to amazon S3 (by streaming method). So you can return a path in jsonResponse. – Ramesh Murugesan Mar 05 '15 at 07:19

3 Answers3

4

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();
};
Piotr Leniartek
  • 1,177
  • 2
  • 14
  • 33
  • @leora, based on the description of your issue, this answer should solve your issue. Do an HTTP Post to the method above (one that returns a FileResult). This should get around both of your issues just fine. – jason Mar 03 '15 at 20:48
  • I don't understand how the querystring is getting inserted into the form to submit. Can you clarify how I would take a string like this name=Joe&age=33&wieght=200 and pass that into the form submit: – leora Mar 05 '15 at 02:49
  • @leora this is POST so you don't pass querystring, in the POST method parameters are passed in the request body not in the URL, this resolves the problem with characters limit you have. – Piotr Leniartek Mar 05 '15 at 07:58
  • @leora parameters are automatically taken from your form body. If you don't understand POST mechanism maybe try this http://blog.michaelckennedy.net/2012/01/20/building-asp-net-mvc-forms-with-razor/ – Piotr Leniartek Mar 05 '15 at 08:10
0

Similar questions have been answered. I'm not sure if you saw any of these questions:

But both people got answers that they say fixed their problems. Their problems sound similar to what you're looking for.

Community
  • 1
  • 1
j.jerrod.taylor
  • 1,120
  • 1
  • 13
  • 33
0

HTTP headers in the response tell the browser that it is a file coming from the server. In ajax response it is our responsibility to handle the response rather than the browser automatically handling it.

Therefore, either we have to find out how we can invoke the browser download dialog in result of an ajax request.

Or

We have to just capture the file url from one ajax request and then just open that file url using our jquery/javascript code. That will result in a file download dialog.

I hope this is helpful. I will be happy if someone else also adds to this or correct me if I am wrong.

Thanks.

muasif80
  • 5,586
  • 4
  • 32
  • 45