How to save and retrieve images from database using Entity Framework Core in ASP.NET Core web application?

asp.net-core-2.0 c# entity-framework-core sql-server

Question

I am trying to create a web application where I can upload the image and see the uploaded images. The best option I was trying is to store the Images into SQL Server and Load It. How can I achieve this with the help of Entity Framework Core.

1
2
9/22/2018 6:13:31 AM

Accepted Answer

You need to save the image data in a Base64 string in the database and read it from DB then assign to src in img tag.

You can refer the below sample.

C# Code

string imageData = @"data:image / jpeg; base64," + Convert.ToBase64String(File.ReadAllBytes(imgPath));

HTML

<img src=imageData />

Alternative Approach

You can save the images on the server also and read it from there. for this, you need to either disable CORS or allow the header/origin/method in EF Core project.

1
9/22/2018 6:08:28 AM


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