显示 PDF ASP.Net MVC

Nic*_*rca 4 asp.net-mvc

我的桌面上有一个文件供测试。我试图在一个看起来像这样的视图中显示它:

@{
    ViewBag.Title = "ShowFile";
}

<h2>ShowFile</h2>
Run Code Online (Sandbox Code Playgroud)

我用于控制器的代码是:

   [HttpGet]
        public ActionResult ShowFile(string path)
        {
            path = @"C:\Documents and Settings\nickla\Desktop\nlamarca_06_15.pdf";

            return File(path, "application/pdf", "nlamarca_06_15.pdf");
        }
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时,视图会显示“未定义”关于此处可能出错的任何想法?

Dar*_*rov 7

您似乎没有在路径中指定文件名:

public ActionResult ShowFile(string filename)
{
    var path = @"C:\Documents and Settings\nickla\Desktop";
    var file = Path.Combine(path, filename);
    file = Path.GetFullPath(file);
    if (!file.StartsWith(path))
    {
        // someone tried to be smart and sent 
        // ?filename=..\..\creditcard.pdf as parameter
        throw new HttpException(403, "Forbidden");
    }
    return File(file, "application/pdf");
}
Run Code Online (Sandbox Code Playgroud)