【问题标题】:Hard coded path: how do I link it on a ASP.NET MVC website?硬编码路径:如何在 ASP.NET MVC 网站上链接它?
【发布时间】:2014-10-20 15:33:38
【问题描述】:

我在远程 Web 服务器上的以下位置有一个 PDF 文档:

C:\Domains\Bin\Invoices\1000.pdf

如何链接它?我试过了:

string rootPath = Server.MapPath("~");
string path = Path.GetFullPath(Path.Combine(rootPath, "..\\Bin\\Invoices"));

<p>@Html.Raw("<a href='"+ path + "\\" + "1000.pdf'>Original PDF copy</a>")</p>

没有成功:浏览器工具提示显示file:///C:/domains/bin/invoices/1000.pdf

谢谢。

编辑

使用乔建议解决,这样:

public FileResult InvoiceInPdf(string id)
{
    string rootPath = Server.MapPath("~");
    string path = Path.GetFullPath(Path.Combine(rootPath, "..\\Bin\\Invoices\\"));

    return File(path + id + ".pdf", "application/pdf");
}

【问题讨论】:

    标签: asp.net asp.net-mvc url


    【解决方案1】:

    您无法创建超链接以浏览到服务器上任意位置的文件。这是一件好事,否则您的网络服务器的安全性会受到威胁。

    您应该创建一个 Action,通过返回 FileResult 来流式传输您想要的文件。 Google 将为您提供这种方法的示例。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-07
      • 1970-01-01
      • 1970-01-01
      • 2013-08-03
      • 1970-01-01
      • 1970-01-01
      • 2014-06-30
      • 2014-01-10
      相关资源
      最近更新 更多