Reputation: 587
I have the following issue: I have a MVC application, in some action of some controller i'm generating a PDF file, the file is being generated on a specific path on the server. That action is being called on an action link of the view, when the user clicks that link, the action generated that PDF, everything fine until here. I want the page to show the dialog with my generated PDF file that says:
Open - Save - Cancel (the tipical file dialog when you click a file)
But without refreshing the page, only show the dialog when the user clicked the link.
How could i do that? what should the action return to the view? Thanks.
Upvotes: 4
Views: 16386
Reputation: 25
Try something like this
public class PdfResult : ActionResult
{
//private members
public PdfResult(/*prams you need to generate that pdf*/)
public override void ExecuteResult(ControllerContext context)
{
//create the pdf in a byte array then drop it into the response
context.HttpContext.Response.Clear();
context.HttpContext.Response.ContentType = "application/pdf";
context.HttpContext.Response.AddHeader("content-disposition", "attachment;filename=xxx.pdf");
context.HttpContext.Response.OutputStream.Write(pdfBytes.ToArray(), 0, pdfBytes.ToArray().Length);
context.HttpContext.Response.End();
}
}
Then u just return a PdfResult
Tip: I got a generic class for doing this and it's something like this and i am using NFop
public PdfResult(IQueryable source, Dictionary<string,int> columns, Type type)
{
Source = source;
Columns = columns;
SourceType = type;
}
Upvotes: 0
Reputation: 15360
To provide the Open - Save - Cancel dialog you will need to set the appropriate Response headers, and as @RichardOD says, return a FilePathResult or FileStreamResult.
HttpContext.Response.AddHeader("content-disposition", "attachment;
filename=form.pdf");
return new FileStreamResult(fileStream, "application/pdf");
Upvotes: 3
Reputation: 29157
Have a look at FilePathResult and FileStreamResult.. An example is here.
Upvotes: 4