开发者

how to open filecontentresult without window prompt in any browser

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.


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");
    }


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;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜