ASP.Net Core保存base64字符串 [英] ASP.Net Core saving base64 string

查看:696
本文介绍了ASP.Net Core保存base64字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图像模型:

public class Image
{
    [Key]
    public long ImagelId { get; set; }
    public string base64 { get; set; }
}

我使用以下方式:

public class CoreGoal
{
    [Key]
    public long CoreGoalId { get; set; }
    [Required]
    public string Title { get; set; }

    public virtual ICollection<Image> Images { get; set; }


    public CoreGoal()
    {

    }
}

我正在使用MySql数据库.我打算针对每个CoreGoal将多个图像存储为base64字符串.

I am using MySql database. I intend to store possibly multiple images as base64 strings against each CoreGoal.

每当我使用base64字符串发出POST请求时,都会成功,但是在保存到数据库时会截断大部分字符串.

Whenever I make a POST request with base64 strings, it is successful, however a large portion of the the string is chopped while saving to the database.

我使用了错误的数据结构来存储base64即字符串吗?这是我的ASP.Net代码中的问题还是MySql的限制?

Am I using a wrong data structure for storing base64 i.e. string? Is it a problem in my ASP.Net code or is it a MySql limitation?

我该如何解决?

更新:

我的存储库类:

public class CoreGoalRepository : ICoreGoalRepository
{
    private readonly WebAPIDataContext _db;

    public CoreGoalRepository(WebAPIDataContext db)
    {
        _db = db;
    }

    //Add new
    public void CreateCoreGoal(CoreGoal coreGoal)
    {
        _db.CoreGoals.Add(coreGoal);
        _db.SaveChanges();
    }

    //Get all
    public IEnumerable<CoreGoal> GetAllCoreGoals()
    {
        return _db.CoreGoals
            .Include(coreGoal => coreGoal.Benefits)
            .Include(coreGoal => coreGoal.Images)
            .ToList();
    }
}

public interface ICoreGoalRepository
{
    void CreateCoreGoal(CoreGoal coreGoal);
    IEnumerable<CoreGoal> GetAllCoreGoals();
}

我的控制器:

 [Route("api/[controller]")]
    public class CoreGoalController : Controller
    {
        private readonly ICoreGoalRepository _coreGoalRepository;

        //Controller
        public CoreGoalController(ICoreGoalRepository coreGoalRepository) {
            _coreGoalRepository = coreGoalRepository;
        }

        //Get methods
        [HttpGet]
        public IEnumerable<CoreGoal> GetAll()
        {
            return _coreGoalRepository.GetAllCoreGoals();
        }

        //Create
        [HttpPost]
        public IActionResult Create([FromBody] CoreGoal item)
        {
            if (item == null)
            {
                return BadRequest();
            }

            _coreGoalRepository.CreateCoreGoal(item);

            return CreatedAtRoute("GetCoreGoal", new { id = item.CoreGoalId }, item);
        }
     }

推荐答案

发出发布请求时,还返回以JSON创建的对象,因此您可以看到URL,或在发布中放置断点并查看base64字符串的值.

When you make the post request return also the object created as a JSON, so you can see the URL, or put a break point in your post and see the value of base64 string.

如果URL正确,则为Db,因此您需要使用[StringLength(length)]属性手动设置URL的最大大小.

If the URL is ok then its the Db, so you need to manually set the max size of the URL with the attribute [StringLength(length)]

这篇关于ASP.Net Core保存base64字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆