gpt4 book ai didi

asp.net-mvc - 显示 PDF ASP.Net MVC

转载 作者:行者123 更新时间:2023-12-01 10:05:19 24 4
gpt4 key购买 nike

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

@{
ViewBag.Title = "ShowFile";
}

<h2>ShowFile</h2>

我用于 Controller 的代码是:
   [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");
}

当我运行此代码时, View 会显示“未定义”关于此处可能出现什么问题的任何想法?

最佳答案

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

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");
}

关于asp.net-mvc - 显示 PDF ASP.Net MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11524293/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com