ASP Core Razor-如何将文件上传到SQL数据库?

时间:2019-03-01 22:10:06

标签: razor asp.net-core file-upload entity-framework-core

很久以前,我使用ASP.NET创建了一个用于工作的应用程序。我现在正在使用Razor页面(ASP.NET核心)重写应用程序。如果问题太简单或在其他地方已经回答,我深表歉意。我已经搜寻了几天!我在将文件上传到表时遇到困难。

模型如下:

using System;
using System.Collections.Generic;

namespace FinplianceProject.Models
{
    public partial class TblSdgs
    {
        public int IntSdgId { get; set; }
        public string StrSdgShort { get; set; }
        public string StrSdgLong { get; set; }
        public byte[] ImgSdgIconNormal { get; set; }
        public byte[] ImgSdgIconInverted { get; set; }
    }
}

我的剃须刀页面如下:

@page
@model FinplianceProject.Pages.SDGs.EditModel

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

<h2>Edit</h2>

<h4>TblSdgs</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form method="post"  enctype="multipart/form-data" asp-controller="UploadFiles" asp-action="Index">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <input type="hidden" asp-for="TblSdgs.IntSdgId" />
            <div class="form-group">
                <label asp-for="TblSdgs.StrSdgShort" class="control-label"></label>
                <input asp-for="TblSdgs.StrSdgShort" class="form-control" />
                <span asp-validation-for="TblSdgs.StrSdgShort" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="TblSdgs.StrSdgLong" class="control-label"></label>
                <input asp-for="TblSdgs.StrSdgLong" class="form-control" />
                <span asp-validation-for="TblSdgs.StrSdgLong" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="TblSdgs.ImgSdgIconNormal" class="control-label"></label>
                <input type="file" asp-for="TblSdgs.ImgSdgIconNormal" />
            </div>
            <div class="form-group">
                <label asp-for="TblSdgs.ImgSdgIconInverted" class="control-label"></label>
                <input type="file" asp-for="TblSdgs.ImgSdgIconInverted" />
            </div>
            <div class="form-group">
                <input type="submit" value="Save" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>

<div>
    <a asp-page="./Index">Back to List</a>
</div>

@section Scripts {
    @{await Html.RenderPartialAsync("_ValidationScriptsPartial");}
}

后端看起来像这样:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using FinplianceProject.Models;

namespace FinplianceProject.Pages.SDGs
{
    public class EditModel : PageModel
    {
        private readonly FinplianceProject.Models.DB_19456_ccsolutions1Context _context;

        public EditModel(FinplianceProject.Models.DB_19456_ccsolutions1Context context)
        {
            _context = context;
        }

        [BindProperty]
        public TblSdgs TblSdgs { get; set; }

        public async Task<IActionResult> OnGetAsync(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }

            TblSdgs = await _context.TblSdgs.FirstOrDefaultAsync(m => m.IntSdgId == id);

            if (TblSdgs == null)
            {
                return NotFound();
            }
            return Page();
        }

        public async Task<IActionResult> OnPostAsync()
        {
            if (!ModelState.IsValid)
            {
                return Page();
            }

            _context.Attach(TblSdgs).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TblSdgsExists(TblSdgs.IntSdgId))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }

            return RedirectToPage("./Index");
        }

        private bool TblSdgsExists(int id)
        {
            return _context.TblSdgs.Any(e => e.IntSdgId == id);
        }
    }
}

该页面显示“确定”,并带有有效的文件上传控件,但是当我单击“保存”时,文件不会保存。

请先帮助并感谢!

0 个答案:

没有答案