ASP在浏览器内部以浏览模式打开PDF而不是显示下载提示

时间:2017-01-05 05:18:15

标签: c# asp.net asp.net-mvc pdf

考虑一个必须为网站用户提供帮助的UserGuide PDF文件。

以下是视图中的链接:

@Html.ActionLink("User Guide","Help","Portal")

返回文件的控制器:

public ActionResult Help()
    {
        string path = Server.MapPath("~/Content/UserGuide.pdf");
        var fileBytes = System.IO.File.ReadAllBytes(path);
        var responseFile = new FileContentResult(fileBytes, "application/pdf")
        {
            FileDownloadName = "Guide.pdf"
        };
        return responseFile;
    }

但是在这里,用户点击链接后,浏览器会提示下载文件,但更好的方法是,当用户点击链接时,PDF页面会显示在浏览器中。就像一个网页!任何机构都能提到实现这一目标的最佳方法吗?

1 个答案:

答案 0 :(得分:1)

看起来你需要返回一个FileStreamResult,例如

Response.AppendHeader("content-disposition", "inline; filename=file.pdf");
return new FileStreamResult(stream, "application/pdf")

来自SO here

或者您可以尝试FileResult,例如

return File(fileArray, contentType, fileName)

来自SO here