首页 > 解决方案 > 在特定页面上的 iframe 中打开 PDF 文件

问题描述

我有一个 iframe,它使用 Url.Action 从动作控制器打开 PDF。是否可以在特定页面上打开此 PDF?由模型中的值确定,例如 @Model.FilePage

框架:

        <iframe width="700" height="1600" src="@Url.Action("OpenPDF", new { id = 8 })"></iframe>

控制器动作:

public ActionResult OpenPDF(int? id)
        {
            CompletedCamp completedCamp = db.CompletedCamps.Find(id);
            string filepath = Server.MapPath(Path.Combine("~/Surveys/" + completedCamp.SurveyName));
            return File(filepath, "application/pdf");
        }

标签: c#asp.netasp.net-mvciframe

解决方案


在特定页面上打开,add #page=[page number]到 src 的末尾

<iframe width="700" height="1600" src="@Url.Action("OpenPDF", new { id = 8 })#page=4"></iframe>

如果页码应该来自模型,请执行

public ActionResult Index()
{
    MyViewModel m = new MyViewModel() { FilePage = 4 }; 
    return View(m);
}

...
@model MVC.MyViewModel 

<iframe width="700" height="1600" src="@Url.Action("OpenPDF", new { id = 8 })#page=@Model.FilePage"></iframe>

推荐阅读