iframe在ASP MVC中加载PDF文件时显示ActionMethod名称 [英] Iframe shows ActionMethod name while loading PDF file in ASP MVC

查看:168
本文介绍了iframe在ASP MVC中加载PDF文件时显示ActionMethod名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

i am using iframe tag in order to call ActionMethod that returns FileResult to display PDF file. The issue is after it loads PDF document instead of showing pdf file name, it shows ActionMethod name on the top of the PDF file name in Chrome.

Razor代码:

<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'。

Image: As you can see in the screenshot, it shows 'GetAgreementToReview' i.e. ActionMethod name instead of 'Master-Agreement.pdf'.

有谁知道如何解决这个问题?
谢谢。

Does anyone know how to fix this issue? Thanks.

Sanjeev

推荐答案

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

I had a similar problem and found a solution after exploring almost everything the web has to offer.

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

You must use a custom route for your action and you must send the filename from UI into the URL itself. (Other parameters won't be affected)

//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。
它有点hacky,但它确实有效。

And on client-side, you can use whatever sistem to reach the URL. It's a bit hacky, but it works.

如果你在HTML上有一个iframe来显示PDF(我遇到的问题),它可能看起来像这个:

If you have an Iframe on HTML to display the PDF (the issue I had), it could look like this:

<iframe src='/ControllerName/GetAgreementToReview/my file?exampleParameter=5' />

这样你就可以使用dinamyc方式操纵IFRAME显示的文件名,只使用最后一个网页上显示其名称的URL的一部分(非常烦人)。下载的文件名将从服务器端Content-disposition给出其名称。

And this way you have a dinamyc way of manipulatic the filename shown by the IFRAME that only uses the last part of an URL for its name shown on the web page (quite annoying). The downloaded filename will have its name given from server-side Content-disposition.

希望它有所帮助!

这篇关于iframe在ASP MVC中加载PDF文件时显示ActionMethod名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆