我有个问题.我将一些图像存储在DB中作为base64,现在我需要编辑包含此图像的此对象.用户在表单中上传图像,然后将其转换为base64并将其存储在DB中.现在我的问题很热,将base64图像转换回IFormFile以显示它以编辑整个对象.
日Thnx
解决方法
If you’re trying to get a object/viewmodel that contains a Byte[]/base64,
i searched in hours for a solution but then i added extra parameter to my viewmodel
public class ProductAddVM { public int Id { get; set; } public Categories Category { get; set; } public decimal Vat { get; set; } public string Name { get; set; } public decimal Price { get; set; } public IFormFile Image { get; set; } public Byte[] ByteImage { get; set; } public string Description { get; set; } public bool? Available { get; set; } }
参数Image用于存储可能正如您所述在EDIT中上传的新图像.
而参数ByteImage是从数据库中获取旧图像.
The where you’re done editing you can convert the IFormFile to byte[] and save it in DataBase
I tried to use Mapper but it went wrong,this code is working 100% but i’m gonna make it look better
internal ProductAddVM GetProduct(int id) { var model = new Product(); model = Product.FirstOrDefault(p => p.Id == id); var viewmodel = new ProductAddVM(); viewmodel.Id = model.Id; viewmodel.Name = model.Name; viewmodel.Available = model.Available; viewmodel.Description = model.Description; viewmodel.Price = model.Price; viewmodel.Category = (Categories)model.Category; viewmodel.Vat = model.Vat; viewmodel.ByteImage = model.Image; return viewmodel; } internal void EditProduct(int id,ProductAddVM viewmodel,int userId) { var tempProduct = Product.FirstOrDefault(p => p.Id == id); tempProduct.Name = viewmodel.Name; tempProduct.Available = viewmodel.Available; tempProduct.Description = viewmodel.Description; tempProduct.Price = viewmodel.Price; tempProduct.Category =(int)viewmodel.Category; tempProduct.Vat = CalculateVat(viewmodel.Price,(int)viewmodel.Category); if (viewmodel.Image != null) { using (var memoryStream = new MemoryStream()) { viewmodel.Image.copyToAsync(memoryStream); tempProduct.Image = memoryStream.ToArray(); } } tempProduct.UserId = userId; tempProduct.User = User.FirstOrDefault(u => u.Id == userId); SaveChanges(); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。