首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ASP MVC中加载ActionMethod文件时,Iframe显示PDF名称

在ASP MVC中加载ActionMethod文件时,Iframe显示PDF名称
EN

Stack Overflow用户
提问于 2016-01-07 23:14:49
回答 2查看 2.2K关注 0票数 0

我使用iframe标签来调用返回FileResult的ActionMethod来显示PDF文件。这个问题是在加载PDF文档后,它在Chrome中的ActionMethod文件名的顶部显示pdf文件名而不是PDF文件名。

剃刀代码:

代码语言:javascript
复制
<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代码:

代码语言:javascript
复制
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

EN

回答 2

Stack Overflow用户

发布于 2016-12-07 18:22:17

我遇到了类似的问题,在探索了几乎所有网络提供的东西后,我找到了一个解决方案。

您必须对操作使用自定义路由,并且必须将文件名从UI发送到URL本身。(其他参数不受影响)

代码语言:javascript
复制
//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 (我遇到的问题),它可能是这样的:

代码语言:javascript
复制
<iframe src='/ControllerName/GetAgreementToReview/my file?exampleParameter=5' />

这样,你就有了一种动态的方式来操作IFRAME所显示的文件名,它只使用URL的最后一部分作为其在网页上显示的名称(相当烦人)。下载的文件名的名称来自服务器端的Content-disposition。

希望它能帮上忙!

票数 3
EN

Stack Overflow用户

发布于 2016-01-08 00:01:33

请尝试此代码:

代码语言:javascript
复制
return File("~/Content/Master-Agreement.pdf", "application/pdf", "filename.pdf");

其他帮助链接:Stream file using ASP.NET MVC FileContentResult in a browser with a name?

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34658690

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档