在ASP.NET Core中下载文件

时间:2018-04-12 17:17:08

标签: postgresql asp.net-core

我尝试在PostgreSQL和ASP.NET Core上编写一些小系统。我需要一个下载文件的功能,它保留在数据库上。

这是代码

namespace WebApplication4.Entites
{
    public class Documents : BaseEntity
    {
        public Guid DocumentId { get; set; } = new Guid();

        public String DocumentName { get; set; }

        public byte[] Contents { get; set; }

        public String DocumentIntroNumber { get; set; }

        public DateTime DateIntro { get; set; }
    }
}

这是控制器

namespace WebApplication4.Controllers
{
    public class DocumentsController : Controller
    {
        private readonly DocumentsRepository dRepository;
        private string connectionString;

        public DocumentsController(IConfiguration configuration, IConfiguration iconfiguration)
        {
            dRepository = new DocumentsRepository(configuration);
            connectionString = iconfiguration.GetValue<string>("DBInfo:ConnectionString");
        }

        internal IDbConnection Connection
        {
            get
            {
                return new NpgsqlConnection(connectionString);
            }
        }


        public IActionResult Index()
        {
            return View(dRepository.FindAll());
        }

        // GET:/Documents/GetFile/1
        public FileStreamResult GetFile(string FileID)
        {
            return GetFile(new Guid(FileID));
        }

        public FileStreamResult GetFile(Guid id)
        {
            using (IDbConnection dbConnection = Connection)
            {
                dbConnection.Open();
                DynamicParameters dynamicParameters = new DynamicParameters();
                Documents result = dbConnection.Query<Documents>("SELECT * FROM selectdocument(@DocumentId)", new { DocumentId = id }).FirstOrDefault();
                Stream stream = new MemoryStream(result.Contents);
                return new FileStreamResult(stream, result.DocumentName);
            }
        }
    }
}

和Index.cshtml

model IEnumerable<WebApplication4.Entites.Documents>

@{
    ViewData["Title"] = "Index";
}

<h2>Index</h2>

<p>
    <a asp-action="Create">Добавить новый документ</a>
</p>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.DocumentName)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DocumentIntroNumber)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.DateIntro)
        </th>
        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.DocumentName)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DocumentIntroNumber)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.DateIntro)
            </td>
            <td>
                <a asp-action="Edit" asp-route-id="@item.DocumentId">Edit</a> |
                <a asp-action="@Url.Action("GetFile","Documents",new {DocumentId = item.DocumentId})">Загрузить файл</a>
            </td>
        </tr>
    }
</table>

但系统在网页上出错

找不到页面localhost     找不到地址页面 http://localhost:59494/Documents/%2FDocuments%2FGetFile%3FDocumentId%3D00000000-0000-0000-0000-000000000000

请帮我解决这个问题。或者告诉我,我在哪里犯了错误?

0 个答案:

没有答案