1

I have a PDF file on the server that I want to send back to the browser as a download, but instead, it just loads to the screen. This is what I'm doing:

public ActionResult BillOfMaterials(Model model)
{
    ....
    return File(@"C:\...(file path)...\result.pdf", "application /pdf");
}

What do I need to do so that it actually just downloads, and doesn't open in the browser?

Thanks!

Casey Crookston
  • 13,016
  • 24
  • 107
  • 193
  • 3
    Is this what you are looking for? https://stackoverflow.com/questions/5826649/returning-a-file-to-view-download-in-asp-net-mvc?rq=1 – ElectricRouge Nov 18 '16 at 16:01
  • 1
    Maybe this might help you too: http://stackoverflow.com/questions/8590543/force-browser-to-download-pdf-document-instead-of-opening-it – Karel Tamayo Nov 18 '16 at 16:02

1 Answers1

0

This did it.

            string filepath = @"C:\docs\result.pdf";
            byte[] filedata = System.IO.File.ReadAllBytes(filepath);
            string contentType = System.Web.MimeMapping.GetMimeMapping(filepath);

            var cd = new System.Net.Mime.ContentDisposition
            {
                FileName = "result.pdf",
                Inline = false,
            };

            Response.AppendHeader("Content-Disposition", cd.ToString());

            return File(filedata, contentType);
Casey Crookston
  • 13,016
  • 24
  • 107
  • 193