我使用iframe标签来调用返回FileResult的ActionMethod来显示PDF文件。这个问题是在加载PDF文档后,它在Chrome中的ActionMethod文件名的顶部显示pdf文件名而不是PDF文件名。
剃刀代码:
<iframe src="@Url.Action("GetAgreementToReview", "Employee")" style="zoom: 0.60; -ms-zoom: 1; width: 100%;" width="99.6%" height="420" frameborder="0" id="agreementPdf"></iframe>CS代码:
public ActionResult GetAgreementToReview()
{
Response.AddHeader("Content-Disposition", "inline; filename=Master-Agreement.pdf");
return File("~/Content/Master-Agreement.pdf", "application/pdf");
}图片:正如你在截图中看到的,它显示了'GetAgreementToReview‘,也就是ActionMethod名称,而不是'Master-Agreement.pdf’。

有人知道如何解决这个问题吗?谢谢。
Sanjeev
发布于 2016-12-07 18:22:17
我遇到了类似的问题,在探索了几乎所有网络提供的东西后,我找到了一个解决方案。
您必须对操作使用自定义路由,并且必须将文件名从UI发送到URL本身。(其他参数不受影响)
//add a route property
[Route("ControllerName/GetAgreementToReview/{fileName?}")]
public ActionResult GetAgreementToReview(string fileName, int exampleParameter)
{
byte[] fileData = null; //whatever data you have or a file loaded from disk
int a = exampleParameter; // should not be affected
string resultFileName = string.format("{0}.pdf", fileName); // you can modify this to your desire
Response.AppendHeader("Content-Disposition", "inline; filename=" + resultFileName);
return File(fileData, "application/pdf"); //or use another mime type
}在客户端,您可以使用任何系统访问URL。
这有点老生常谈,但它是有效的。
如果你在HTML上有一个Iframe来显示PDF (我遇到的问题),它可能是这样的:
<iframe src='/ControllerName/GetAgreementToReview/my file?exampleParameter=5' />这样,你就有了一种动态的方式来操作IFRAME所显示的文件名,它只使用URL的最后一部分作为其在网页上显示的名称(相当烦人)。下载的文件名的名称来自服务器端的Content-disposition。
希望它能帮上忙!
发布于 2016-01-08 00:01:33
请尝试此代码:
return File("~/Content/Master-Agreement.pdf", "application/pdf", "filename.pdf");其他帮助链接:Stream file using ASP.NET MVC FileContentResult in a browser with a name?
https://stackoverflow.com/questions/34658690
复制相似问题