asp.net-mvc ASP.NET MVC 在新窗口中打开 pdf 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11100981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET MVC open pdf file in new window
提问by Tulips
I have a MVC application. I need to open the pdf file when user clicks the open button on the page. The filepath where the pdf is stored is read from the database and it is a file on c:. How do I open it in my html code? I have this code:
我有一个 MVC 应用程序。当用户单击页面上的打开按钮时,我需要打开 pdf 文件。存放pdf的文件路径是从数据库中读取的,是c:上的文件。如何在我的 html 代码中打开它?我有这个代码:
<a href="@Model.CertificatePath" target="_blank" class="button3">Open</a>
but this doesn't open my file. What do I have to do? I need to specify somewhere that it is a pdf??
但这不会打开我的文件。我需要做什么?我需要在某处指定它是 pdf 吗?
回答by moribvndvs
You will need to provide a path to an action that will receive a filename, resolve the full path, and then stream the file on disk from the server to the client. Clients out in the web, thankfully, cannot read files directly off your server's file system (unless... are you suggesting @Model.CertificatePathis the path to the file on the remote user'smachine?).
您需要提供一个动作的路径,该动作将接收文件名,解析完整路径,然后将磁盘上的文件从服务器流式传输到客户端。谢天谢地,网络上的客户端无法直接从服务器的文件系统中读取文件(除非……您是否建议远程用户机器@Model.CertificatePath上的文件路径?)。
public ActionResult Download(string fileName)
{
string path = Path.Combine(@"C:\path\to\files", fileName);
return File(path, "application/pdf");
}
Update
更新
If @Model.CertificatePathis the location on the client's actual machine, try:
如果@Model.CertificatePath是客户端实际机器上的位置,请尝试:
<a href="file://@Model.CertificatePath" target="_blank" class="button3">Open</a>
Note that some browsers may have security settings disallowing you from opening local files.
请注意,某些浏览器可能具有禁止您打开本地文件的安全设置。
回答by Jayakaran Theivendramoorthy
Try like this in your View
在您的视图中尝试这样
@Html.ActionLink("View", "ViewPDF", new { target = "_blank" })
Now Link will open in new window. You can write the pdf bytes in ViewPDF controller method
现在链接将在新窗口中打开。您可以在 ViewPDF 控制器方法中写入 pdf 字节
回答by TheAlbear
You could have the link fire a method such as the one below which will then stream your chosen file to the file download rather than opening the pdf in the broswer.
您可以让链接触发一种方法,例如下面的方法,然后将您选择的文件流式传输到文件下载,而不是在浏览器中打开 pdf。
/// <summary>
/// Forces a file to be displayed to the user for download.
/// </summary>
/// <param name="virtualPath"></param>
/// <param name="fileName"></param>
public static void ForceDownload(string virtualPath, string fileName)
{
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.AddHeader("content-disposition", "attachment; filename=" + fileName);
response.WriteFile(virtualPath);
response.ContentType = "";
response.End();
}
回答by John Mitchell
Unfortunately you can't dictate where the PDF will be opened, mainly because you can't guarantee the Adobe Acrobat reader plugin is installed or how it functions.
不幸的是,您无法指定 PDF 的打开位置,主要是因为您无法保证安装了 Adobe Acrobat 阅读器插件或其功能。
You could in theory open a new window, and in that new window have a JavaScript function to open the PDF file, but again you can't guarantee it will open in a embedded window without the plugin, the best you can hope for is "best try".
理论上您可以打开一个新窗口,并且在该新窗口中有一个 JavaScript 函数可以打开 PDF 文件,但是您再次不能保证它会在没有插件的嵌入式窗口中打开,您最好的希望是“最好的尝试”。
回答by wilaponce
Well if your getting the path value and the value is in @Model.CertificatePath
好吧,如果您获取路径值并且该值在 @Model.CertificatePath 中
this wiil not work
这行不通
<a href="@Model.CertificatePath" target="_blank" class="button3">Open</a>
You will need to add this
你需要添加这个
<a href="@Url.Content(Model.CertificatePath)" target="_blank" class="button3">Open</a>
and make sure you path is relative by adding this ~
for example if your path is /Content/pdfs/CertificatePath.pdf
并通过添加此 ~ 确保您的路径是相对的,例如,如果您的路径是 /Content/pdfs/CertificatePath.pdf
it would need to look like
它需要看起来像
~/Content/pdfs/CertificatePath.pdf
~/Content/pdfs/CertificatePath.pdf
This should be the simplest way to make it work. Hope this helps.
这应该是让它工作的最简单的方法。希望这可以帮助。

