2

如图所示,我有来自控制器操作方法的文件内容结果,内容是字节 [] 类型

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

现在,如果文件类型被称为pdf并指定,为什么不直接在adobe reader中打开并提示窗口用/saveas打开。如果我的文件内容结果通过 pdf,我希望它在没有窗口提示的情况下打开。怎么做到呢?上面的代码在mozilla中只提示窗口,在IE中没有提示或打开。

4

2 回答 2

1

诀窍在于内容类型,您已将其设置为磨损。如果浏览器知道如何处理该内容类型,它将打开它:

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

        return File(contents, "application/pdf");
    }
于 2011-07-27T10:59:12.143 回答
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;
    }
于 2013-06-20T05:00:13.177 回答