我目前正在使用pdf库生成pdf服务器端。完成后,我尝试将此pdf返回给用户。
每当我尝试PDF无法打开时,或者如果我将其下载为下载,则下载为损坏的文件。如果我在浏览器中键入pdf的物理地址,则可以正常加载。
在我的控制器中:
return File(nfilePath, "application/pdf");
在这个例子中nfilePath =〜/ PdfStore / CurRep103323842.pdf
我很难接受这么简单的事情,任何建议都会很棒。感谢。
加载部分视图的视图
@model PrintShiftHandover.Models.ShiftHandOver
@{
ViewBag.Title = "ShiftHandOver";
}
<div id="updateRegion">
@{Html.RenderAction("_ReportDetails");}
</div>
部分视图。
@model PrintShiftHandover.Models.MainReportDetail
@{
Layout = string.Empty;
}
<div id="PartialView">
@using (Html.BeginForm())
{
.............这里有一个表格。
}
</div>
控制器。
[HttpPost]
public ActionResult _ReportDetails(Models.MainReportDetail model)
{
var getfile = new CreatePdf();
var nfilePath = getfile.GetFile(model);
return File(nfilePath, "application/pdf");
}
catch(Exception ex)
{
ModelState.AddModelError("",ex.Message);
return View(model);
}
}
生成的pdf包含主视图中生成的HTML,div updateregion内部是pdf编码所在的位置。
答案 0 :(得分:0)
你不能在路径中使用“〜”。
尝试:
//Convert to absolute path
nfilePath = HttpContext.Current.Server.MapPath(nfilePath);
return File(nfilePath, "application/pdf");
答案 1 :(得分:0)
您正在调用@Html.RenderAction
,这实际上是将PDF文件嵌入到输出中。
您需要使用按钮/链接替换它,而不是调用@Html.ActionLink
。所以替换说:
@{Html.RenderAction("_ReportDetails");}
使用:
@Html.ActionLink("_ReportDetails")
或者,您可以放置元刷新标记或jQuery以强制执行文件下载。