我在数据库中存储了用户名和 pdfile 文件的记录,我想显示带有该 pdf 文件路径下载选项的记录列表,我尝试了一些代码,但它仍然没有解决我的问题。通过使用以下代码,它显示下载选项但出现控制台错误
不允许加载本地资源
如果有人有任何其他想法,如何以更好的方式做到这一点
public ActionResult ViewFromDb()
{
ApplicationDbContext db = new ApplicationDbContext();
var list = db.MeetingDetails.ToList();
list.ForEach(x => {
x.filepath = Server.MapPath(Url.Content("~/App_Data/File" + x.filepath));
});
return View(list);
}
@model IEnumerable<ReportManagement.Models.MeetingDetail>
@{
ViewBag.Title = "ViewFromDb";
}
<h2>ViewFromDb</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.UserName)
</th>
<th>
@Html.DisplayNameFor(model => model.filepath)
</th>
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.UserName)
</td>
<td>
<a href="@(item.filepath)">Download</a>
</td>
</tr>
}
</table>
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace ReportManagement.Models
{
public class MeetingDetail
{
[Key]
public int id { get; set; }
public string UserName { get; set; }
public string filepath { get; set; }
[NotMapped]
public HttpPostedFileBase file { get; set; }
}
}
在提出建议后,我又尝试了另一件事,但我不知道如何在视图中显示
//public ActionResult ViewFromDb()
//{
// ApplicationDbContext db = new ApplicationDbContext();
// MeetingDetail ms = new MeetingDetail();
// DirectoryInfo dirinfo = new DirectoryInfo(ms.filepath);
// FileInfo[] files = dirinfo.GetFiles("*.*");
// List<string> lst = new List<string>(files.Length);
// foreach(var item in files)
// {
// lst.Add(item.Name);
// }
// return View(lst);
//}
//public ActionResult DownloadFromDb(string filename)
//{
// MeetingDetail ms = new MeetingDetail();
// if (Path.GetExtension(ms.filepath) == ".pdf")
// {
// string fullpath = Path.Combine(ms.filepath, filename);
// return File(fullpath, "application/pdf");
// }
// else
// return new HttpStatusCodeResult(HttpStatusCode.Forbidden);
//}
//@{
// ViewBag.Title = "ViewFiles";
//}
//<h2>ViewFiles</h2>
//@foreach(var item in ViewData.Model)
//{
@Html.ActionLink((model=>item.Filepath)item, "DownloadFile", new { filename //= item });
//}