如何使用.NET Core和EF将图像文件保存到SQL数据库中



这里有一个问题,标题和描述正确保存在SQL数据库中,但它将图像文件发送为null。我用这种方式做了一些尝试,但没有得到我想要的结果。

我如何打印这个?

我的班级:

public class Workplace
{
[Key]
[Column("WorkplaceId")]
public int WorkplaceId { get; set; }
[NotMapped]
[Column("WorkplaceThumbnailImage")]
public byte[]? WorkpaceThumbnailImage { get; set; }
[Column("WorkplaceTitle")]
public string? WorkplaceTite { get; set; }
[Column("WorkplaceExpanation")]
public string? WorkplaceExplanation { get; set; }
}

管制员岗位方法:

WorkplaceManager workplaceManager = new WorkplaceManager(new EfWorkplaceRepository());
[AllowAnonymous]
[HttpPost]
public IActionResult Index(Workplace p)
{
Workplace workPlace = new Workplace();
MemoryStream memoryStream = new MemoryStream();
foreach (var file in Request.Form.Files)
{
p.WorkplaceTite = file.FileName;
file.CopyTo(memoryStream);
p.WorkpaceThumbnailImage = memoryStream.ToArray();
}
p.WorkpaceThumbnailImage = p.WorkpaceThumbnailImage;
p.WorkplaceTite = p.WorkplaceTite;
p.WorkplaceExplanation = p.WorkplaceExplanation;
workplaceManager.AddWorkplace(p);
_logger.LogWarning("Success");
return RedirectToAction("Index", "Home");                    
}

HTML:

@model EntityLayer.Concrete.Workplace;
@using (Html.BeginForm("Workplaces", "Home", FormMethod.Post, new { enctype = "multipart / form - data"}))
{
<div class="form-group col-md-12">
<label for="inputText">Add</label>
<input type="File" class="form-control" id="file" name="file1" multiple="multiple"
asp-controller="Home" @Html.DisplayFor(model => model.WorkpaceThumbnailImage) method="post"/>
</div>
}

我的类:

public class Workplace
{
[Key]
[Column("WorkplaceId")]
public int WorkplaceId { get; set; }

[NotMapped]
[Column("WorkplaceThumbnailImage")]
public byte[]? WorkpaceThumbnailImage { get; set; }

[Column("WorkplaceTitle")]
public string? WorkplaceTite { get; set; }

[Column("WorkplaceExpanation")]
public string? WorkplaceExplanation { get; set; }
}

控制柱方法:

WorkplaceManager workplaceManager = new WorkplaceManager(new EfWorkplaceRepository());
[AllowAnonymous]
[HttpPost]
public async Task<IActionResult> Index(Workplace p)
{
Workplace workPlace = new Workplace();
MemoryStream memoryStream = new MemoryStream();
foreach (IFormFile file in Request.Form.Files)
{
workPlace.WorkplaceTite = file.Name;
file.CopyToAsync(memoryStream);
p.WorkplaceThumbnailImage = memoryStream.ToArray();

workPlace.WorkplaceThumbnailImage = workPlace.WorkplaceThumbnailImage;
p.WorkplaceTite = p.WorkplaceTite;
p.WorkplaceExplanation = p.WorkplaceExplanation;
}
workplaceManager.AddWorkplace(p);

_logger.LogWarning("Success");
return RedirectToAction("Index", "Home");
}

Html:

@model EntityLayer.Concrete.Workplace;
@using (Html.BeginForm("Workplaces", "Home", FormMethod.Post, new { enctype = "multipart/form-data"}))
{
<div class="col-lg-5">
<div class="card text-white rounded-3 sm-3">
<button class="btn card text-white rounded-3" type="submit" asp-area="" id="create" asp-controller="Home" asp-action="Hairdresser" style="background-color: #5B2480;">Add</button>
</div>
</div>
}

这就是我根据您的建议编辑代码的方式。它完美地工作。感谢那些做出贡献的人。我意识到这个问题是由";BeginForm";。

。。。您正在对WorkpaceThumbnailImage属性使用[NotMapped],该属性将其从数据库映射中排除
尝试删除它。


编辑
尝试其他方法:

public class Workplace
{
[Key]
[Column("WorkplaceId")]
public int WorkplaceId { get; set; }
[Column("WorkplaceThumbnailImage")]
public byte[]? WorkpaceThumbnailImage { get {
if(this.MyFile is null) 
return null;
using var ms = new MemoryStream();
this.MyFile.CopyTo(ms);
return ms.ToArray();
} }
[NotMapped]
public IFormFile MyFile { get; set; }
[Column("WorkplaceTitle")]
public string? WorkplaceTite { get; set; }
[Column("WorkplaceExpanation")]
public string? WorkplaceExplanation { get; set; }
}  

控制器:

WorkplaceManager workplaceManager = new WorkplaceManager(new EfWorkplaceRepository());
[AllowAnonymous]
[HttpPost]
public IActionResult Index([FromForm] Workplace p)
{
workplaceManager.AddWorkplace(p);
_logger.LogWarning("Success");
return RedirectToAction("Index", "Home");                    
}  

视图:
(将idname属性重命名为MyFile,因为它应该与类的属性名匹配(

@model EntityLayer.Concrete.Workplace;
@using (Html.BeginForm("Workplaces", "Index", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<div class="form-group col-md-12">
<label for="MyFile">Add</label>
<input type="file" class="form-control" id="MyFile" name="MyFile" />
</div>
}

相关内容

  • 没有找到相关文章

最新更新