Asp.net核心2.0文件上传null引用异常

时间:2017-10-25 11:43:12

标签: file-upload asp.net-core-2.0

我正在尝试使用entityframework核心创建用于创建图书数据并将其保存到数据库的表单。

到目前为止无法做到这一点,因为我一直为我的缩略图属性获​​取空引用异常。以下是我的代码:

我的模特:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MyLibrary.Models
{
    public class Book : BaseEntity
    {
        public string Title { get; set; }

        public string ISBN { get; set; }

        public string Author { get; set; }

        public string Publisher { get; set; }

        public int Page { get; set; }

        public byte[] Thumbnail { get; set; }
    }
}

我的CreateBook ViewModel:

using MyLibrary.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace MyLibrary.ViewModels
{
    public class BookCreateViewModel
    {
        public string Title { get; set; }

        public string ISBN { get; set; }

        public string Author { get; set; }

        public string Publisher { get; set; }

        public int Page { get; set; }

        public IFormFile Thumbnail { get; set; }
    }
}

IFormFile接口:

using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace MyLibrary.Data
{
    public interface IFormFile
    {
        string ContentType { get; }
        string ContentDisposition { get; }
        IHeaderDictionary Headers { get; }
        long Length { get; }
        string Name { get; }
        string FileName { get; }
        Stream OpenReadStream();
        void CopyTo(Stream target);
        Task CopyToAsync(Stream target, CancellationToken cancellationToken);
    }
}

CreateBook Controller方法:

    [HttpPost]
    [ValidateAntiForgeryToken]
    public IActionResult Create([Bind("Title,ISBN,Author,Publisher,Page,Thumbnail")] BookCreateViewModel model)
    {
        if (ModelState.IsValid)
        {
            var book = new Book
            {
                 Title=model.Title,
                 ISBN=model.ISBN,
                 Author=model.Author,
                 Publisher=model.Publisher,
                 Page=model.Page
            };

            using (var memoryStream = new MemoryStream())
            {
                model.Thumbnail.CopyTo(memoryStream);
                book.Thumbnail = memoryStream.ToArray();
            }

            _bookRepo.Insert(book);
            return RedirectToAction(nameof(Index));
        }
        return View(model);
    }

最后我的创建视图:

@model MyLibrary.ViewModels.BookCreateViewModel

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

<h2>Create</h2>

<h4>Book</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Create" enctype="multipart/form-data">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>
            <div class="form-group">
                <label asp-for="Title" class="control-label"></label>
                <input asp-for="Title" class="form-control" />
                <span asp-validation-for="Title" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="ISBN" class="control-label"></label>
                <input asp-for="ISBN" class="form-control" />
                <span asp-validation-for="ISBN" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Author" class="control-label"></label>
                <input asp-for="Author" class="form-control" />
                <span asp-validation-for="Author" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Publisher" class="control-label"></label>
                <input asp-for="Publisher" class="form-control" />
                <span asp-validation-for="Publisher" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Page" class="control-label"></label>
                <input asp-for="Page" class="form-control" />
                <span asp-validation-for="Page" class="text-danger"></span>
            </div>

            <div class="form-group">
                <label asp-for="Thumbnail" class="control-label"></label>
                <input type="file" name="Thumbnail" class="form-control" />
                <span asp-validation-for="Thumbnail" class="text-danger"></span>
            </div>

            <div class="form-group">
                <input type="submit" value="Create" class="btn btn-default" />
            </div>
        </form>
    </div>
</div>

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

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

以下是我的表格和例外:

Create form

Exception

你能指出我在这里做错了什么吗?谢谢。

0 个答案:

没有答案