0

我在数据库中存储了用户名和 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 });

//}
4

3 回答 3

0

实际上,据我所知,出于安全目的,IIS 服务器会阻止从 ~/App_Data/ 获取任何文件。所以我们需要将文件作为 FileStream 返回。这段代码对我有用。

var fileSavePath = Path.Combine(HttpContext.Server.MapPath("~/App_Data/FilePath/" + DocPath));
 return File(new FileStream(fileSavePath, FileMode.Open, FileAccess.Read), "application/pdf");
于 2020-01-05T10:57:41.853 回答
0

我看到你使用了 App_Data 文件夹,这个文件夹的目的之一是它的内容不能从网络访问,而是从网络应用程序访问。这就是为什么将数据库等放在那里的原因。不应从网络访问的东西。

尝试创建另一个文件夹(如 /Content)并将文件放在那里。

如果文件不以物理形式存在,而是作为数据库或 blob 存储中的字节存在,则必须创建一个端点,作为客户端和数据存储之间的代理。像这样: ASP.NET MVC: FileStreamResult 返回太多字节?

于 2020-01-05T07:45:03.427 回答
0

试试下面的代码:

x.filepath = Server.MapPath("~/App_Data/File" + x.filepath+".pdf");

于 2020-01-05T07:46:15.777 回答