使用ASP.NET MVC中的实体框架将图片上载到SQL Server数据库



我一直在尝试使用实体框架将图片上传到SQL Server数据库。

我试图拍摄照片并将其从DtoModel中的IFormFile转换为主模型中的byte[]以将其存储在数据库中,但它不起作用;无效值";。

我在ASP.NETWebneneneba API中使用了很多次这种逻辑方法,它运行得很顺利,所以我不知道我错过了什么。

主要型号:

[Key]
public int Id { get; set; }

public string Name { get; set; }

[Required(ErrorMessage = "Photo is required.")]
public byte[] Pic { get; set; }
public string PicFromat { get; set; }

DtoModel:

public string Name { get; set; }
public IFormFile Pics { get; set; }

控制器:

private readonly ApplicationDbContext _context;
private new List<string> _allowedExtenstions = new List<string> { ".jpg", ".png" }; 
private long _maxAllowedPosterSize = 1048576;
public propertiesController(ApplicationDbContext context)
{
_context = context;
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create( dtoprop model)
{
if (!ModelState.IsValid)
{
return View( model);
}
if(!_allowedExtenstions.Contains(Path.GetExtension(model.Pics.FileName.ToLower())))
{
ModelState.AddModelError("Pic", "Only .PNG, .JPG images are allowed!");

return View(model);
}

if (model.Pics.Length > _maxAllowedPosterSize)
{
ModelState.AddModelError("Pic", "Poster cannot be more than 1 MB!");
return View(model);
}
using var dataStream = new MemoryStream();
await model.Pics.CopyToAsync(dataStream);
var pic = new property
{
Name = model.Name,
Pic = dataStream.ToArray()
};
_context.Properties.Add(pic);
_context.SaveChanges();
return RedirectToAction("Index");
}

Create.cshtml:

@model EshopTest5.Data.dtoprop
@{
ViewData["Title"] = "Create";
}
<h1>Create</h1>
<h4>property</h4>
<hr />
<div class="row">
<div class="col-md-4">
<form asp-action="Create">
<div asp-validation-summary="ModelOnly" class="text-danger"></div>
<div class="form-group">
<label asp-for="Name" class="control-label"></label>
<input asp-for="Name" class="form-control" />
<span asp-validation-for="Name" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="PicFromat" class="control-label"></label>
<input asp-for="PicFromat" class="form-control" />
<span asp-validation-for="PicFromat" class="text-danger"></span>
</div>

<div class="custom-file">
<input type="file" class="custom-file-input" asp-for="Pic" accept=".png, .jpg" />
<label class="custom-file-label" asp-for="Pic"></label>
<span asp-validation-for="Pic" class="text-danger"></span>
</div>


<div class="form-group">
<input type="submit" value="Create" class="btn btn-primary" />
</div>
</form>
</div>
</div>
<div>
<a asp-action="Index">Back to List</a>
</div>


我个人建议您为_allowedExtenstions添加客户端验证,而不仅仅是服务器端验证,这样用户就不会浪费他/她的数据包将表单数据发布到服务器,只为了返回验证错误

[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create( dtoprop model)
{
if (!ModelState.IsValid)
{
return View( model);
}
var stream = new MemoryStream();
model.Pics.File.CopyTo(stream);
stream.Position = 0;
var byteData = stream.ToArray();
var pic = new property
{
Name = model.Name,
Pic = byteData
};
_context.Properties.Add(pic);
_context.SaveChanges();
return RedirectToAction("Index");
}

并且您应该将enctype="multipart/form-data" method="post"添加到您的html表单中。

最新更新