3

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.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
Ashish Rathi
  • 1,418
  • 2
  • 13
  • 26
  • 2
    You can save the image as byte stream in database but this is not the best solution. It's better to save the file on disk and store path of it in database or use FileStream feature of SQL Server. – Mojtaba Tajik Sep 22 '18 at 05:58
  • @MojtabaTajik - Why it is not the best solution? – Ashish Rathi Sep 22 '18 at 06:07
  • Read this : https://softwareengineering.stackexchange.com/questions/150669/is-it-a-bad-practice-to-store-large-files-10-mb-in-a-database – Mojtaba Tajik Sep 22 '18 at 06:11

1 Answers1

2

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.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197