Home > Mobile >  How can I store an image in MySQL database using Razor Pages?
How can I store an image in MySQL database using Razor Pages?

Time:04-11

I am learning web development and working on Razor Pages. For my last assignment I was asked to make a website. In my website people can upload images and others see these images. The problem is I dont know how to store those photos. My tutor said if I save them in my sql table as binary values, that makes it very slow. I have to find another way. Can I convert image inputs to Url and store them as "text" ? Or is there any other solution?

CodePudding user response:

You can save the file to the file system (wwwroot/images, for example) and the file name of the image in the database. Then construct the value for the src attribute from the saved filename:

<img src="/images/@Model.ImageFileName" alt="..." />

My tutor said if I save them in my sql table as binary values, that makes it very slow.

I can't speak with any authority on MySQL, but you should test to see if there is actually a performance penalty, and if so, whether it's significant.

CodePudding user response:

Store the Uploaded files in the File System and insert the path of the saved file in the DB.

This will help you to maintain the DB performance.

Thanks

  • Related