ASP.Net Core saving base64 string

asp.net-core asp.net-core-mvc entity-framework-core

Question

I have a Image model:

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

Which I use like following:

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

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


    public CoreGoal()
    {

    }
}

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

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.

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?

How can I fix this?

UPDATE:

My Repository class:

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();
}

My controller:

 [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);
        }
     }
1
1
3/31/2017 5:22:17 PM

Popular Answer

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.

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)]

1
3/31/2017 2:38:17 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow