2

I have filecontentresult from controller action method as shown ,contents is byte[] type

FileContentResult file= new FileContentResult(contents, "/PDF");
              Response.AppendHeader("Content-Disposition", "inline; filename=" + filename);
                return file;

Now, if the file type is known as pdf and specified, why is not directly opening in adobe reader and prompting window to openwith /saveas. If my filecontentresult passes pdf I want it to open without window propmt. how can it be done? Also the above code only prompting window in mozilla, In IE no prompt or opening.

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
michael
  • 575
  • 2
  • 14
  • 27
  • 1
    this is not an mvc issue, this is a browser issue and how it manages pdf's (maybe it doesn't support the PDF browser-plugin) – Vasea Jul 27 '11 at 08:55

2 Answers2

1

The trick is in content type, you've set it worng. If browser knows how to handle that content type it will open it:

    public ActionResult GetPDF()
    {
        var path = @"C:\Test\Testing.pdf";
        var contents = System.IO.File.ReadAllBytes(path);

        return File(contents, "application/pdf");
    }
frennky
  • 12,581
  • 10
  • 47
  • 63
0

The answer in one line.

return new FileContentResult(documentModel.DocumentData, documentModel.DocumentMediaType);

And to put it into context here is the DocumentSave...

    private bool SaveDocument(DwellingDocumentModel doc, HttpPostedFileBase files)
    {
        if (Request.Files[0] != null)
        {
            byte[] fileData = new byte[Request.Files[0].InputStream.Length];
            Request.Files[0].InputStream.Read(fileData, 0, Convert.ToInt32(Request.Files[0].InputStream.Length));

            doc.DocumentData = fileData;
            doc.DocumentMediaType = Request.Files[0].ContentType;
        }

        if (doc.Save())
        {
            return true;
        }

        return false;
    }
Atters
  • 801
  • 8
  • 19