我正在使用ASP.NET Core(MVC 6)EF Visual Studio开发一个小博客.我无法找到如何将图像保存到数据库.我已经读过有关IFormfile但我真的不明白如何去做,我被卡住了.我是新手,很想得到一些帮助.
我想将图像保存到我正在创建的帖子中(以相同的形式).因此,我想将它保存到postID.然后我需要能够显示图像,我该怎么做?我知道有很多要问,但我不知道在哪里转.如果您有任何好的提示或想法,请随时发送链接.
提前致谢!
解决方法
如果您需要保存到数据库,您可能会发现这很有用.这是
https://www.mikesdotnetting.com/article/259/asp-net-mvc-5-with-ef-6-working-with-files的修改,k7Boys的大量输入在这里回答
MVC 6 HttpPostedFileBase?
<input type="file" name="Image" id="Imageinput">
博客模态类应该有Img字段;
public int BlogId{ get; set; } ... public byte[] Img{ get; set; }
控制器;
public async Task<IActionResult> Create([Bind("BlogId,...Img")] Blog blog t,IFormFile Image) if (ModelState.IsValid) { if (Image!= null) { if (Image.Length > 0) //Convert Image to byte and save to database { byte[] p1 = null; using (var fs1 = Image.OpenReadStream()) using (var ms1 = new MemoryStream()) { fs1.copyTo(ms1); p1 = ms1.ToArray(); } Blog.Img= p1; } } _context.Add(client); await _context.SaveChangesAsync(); return RedirectToAction("Index"); }
花了几个小时才到这儿.现在正在查看视图中的图像,我相信这不会很复杂.请享用
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。