在新窗口中的ASP.NET MVC打开PDF文件 [英] ASP.NET MVC open pdf file in new window

查看:1947
本文介绍了在新窗口中的ASP.NET MVC打开PDF文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个MVC应用程序。我需要打开PDF文件时,用户点击页面上的开启按钮。其中PDF格式存储的文件路径,从数据库中读取,它是c上的文件:.如何在我的html code打开呢?我有这个code:

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:

<a href="@Model.CertificatePath" target="_blank" class="button3">Open</a>

但是这并不能打开我的文件。我需要做什么?我需要指定的地方,它是一个PDF?

but this doesn't open my file. What do I have to do? I need to specify somewhere that it is a pdf??

推荐答案

您将需要提供一个操作的路径将接收一个文件名,解决的完整路径,然后从服务器流磁盘上的文件到客户。客户出在网络,幸运的是,无法读取文件直接关闭服务器的文件系统(除非......你暗示 @ Model.CertificatePath 是路径的文件的远程用户的的机器吗?)。

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.CertificatePath is the path to the file on the remote user's machine?).

public ActionResult Download(string fileName)
{
   string path = Path.Combine(@"C:\path\to\files", fileName);

   return File(path, "application/pdf");

}

更新

如果 @ Model.CertificatePath 是客户端的实际计算机上的位置,请尝试:

If @Model.CertificatePath is the location on the client's actual machine, try:

 <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.

这篇关于在新窗口中的ASP.NET MVC打开PDF文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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