如何在任何浏览器中打开没有窗口提示的filecontentresult

时间:2011-07-27 08:42:30

标签: asp.net-mvc browser cross-browser http-headers filecontentresult

我有来自控制器动作方法的filecontentresult,如图所示,内容是byte [] type

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

现在,如果文件类型被称为pdf并指定,为什么不直接在adobe reader中打开并提示窗口openwith / saveas。如果我的filecontentresult传递pdf我希望它打开没有窗口propmt。怎么做到呢?另外上面的代码只提示mozilla中的窗口,在IE中没有提示或打开。

2 个答案:

答案 0 :(得分:1)

诀窍是内容类型,你设置错了。如果浏览器知道如何处理该内容类型,它将打开它:

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

        return File(contents, "application/pdf");
    }

答案 1 :(得分:0)

答案在一行。

返回新的FileContentResult(documentModel.DocumentData,documentModel.DocumentMediaType);

将文档放在上下文中是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;
    }