1

I've created a PdfActionResult class as follows:

public class PdfActionResult : ActionResult
{
    public byte[] FileContents { get; set; }
    public string FileName { get; set; }


    public override void ExecuteResult(ControllerContext context)
    {
        var cd = new System.Net.Mime.ContentDisposition()
                     {
                        FileName = FileName,
                        Inline = false,
                     };

        context.HttpContext.Response.Buffer = true;
        context.HttpContext.Response.Clear();
        context.HttpContext.Response.AppendHeader("Content-Disposition", cd.ToString());
        context.HttpContext.Response.ContentType = "application/pdf";
        context.HttpContext.Response.BinaryWrite(FileContents);
    }

}

I have a controller method that returns a PdfActionResult. This works fine when called from within a view but it fails when called from a partial view. My guess is that it has something to do with the Controller context. Any help would be appreciated. Thanks.

Geri Langlois
  • 696
  • 1
  • 9
  • 21
  • Instead of creating your own Result class, what about using FileResult instead? http://stackoverflow.com/questions/1375486/how-to-create-file-and-return-it-via-fileresult-in-asp-net-mvc – Dave Markle Sep 18 '12 at 01:15
  • @Dave Markle - thanks for the comment but I tried it and it's not working – Geri Langlois Sep 18 '12 at 02:23

2 Answers2

1

Sorry for not answering your question more directly in my comments.

What do you expect to happen by putting this in a partial view? Basically, with files, it's an all or nothing proposition. As soon as you return a FileResult, it's game over. No page is rendered, and the only thing that happens is that a file is streamed over to the client's browser. Basically, in a HTTP stream you can only have one content type -- either it's an HTML document or it's another type of file.

If what you really want is to have a page with custom markup which happens to show a PDF inside a frame, I suggest using an <iframe> tag, then pointing the source of that's iFrame tag to be an action which returns the file.

Dave Markle
  • 95,573
  • 20
  • 147
  • 170
  • Basically, I have a reports page in my app. This page has several jquery tabs - each tab loads a partial view. Fiddler actually shows the download whether I use a FileResult or my PdfActionResult. Inside a view both work - inside a partial view neither works. (I also tried it in a partial view outside of any tab and it did not work.) – Geri Langlois Sep 18 '12 at 11:30
1

Turns out the problem was not the partial view but the fact that I was using an Ajax.BeginForm. It was attempting the download in the Ajax context and that's what caused it to fail. Changing to Html.BeginForm did the trick - hopefully this helps someone in the future.

Geri Langlois
  • 696
  • 1
  • 9
  • 21