How to c # upload image to database? The database has an image type field, and you need to "shove a photo there" from the hard disk, the user specifies the path to the file, and it is sent to the database. Tell me, please, how to do this?
- 2Always sent to the database only the path to the file, much easier. - Specter
- as? and where to store the photos themselves? Well .... and if you store the link in the database, then how, on the basis of this link, upload a photo from the server .... and how to upload it to the server by writing this link ... - Rakzin Roman
- If you still want to store files in the database, then you can use the new FileTable functionality in MS SQL Server 2012, which supports full synchronization of the file system and database. This is so, by the way :) - null
- And how much is it in principle justified to store images in a database instead of a file system, apart from the convenience of working with data? - Bushfighter
2 answers
@Spectre quite rightly says that it is much easier and more convenient to store the path to the image in the database, and the image itself either in the file system or in the CDN. That is, instead of a stream of bytes, you make a text field that stores the path to the file. While working, put the file (or read) somewhere in the file system, and in the table - the path to it.
You read the picture from the file as standard - by calling Image.FromFile or something else.
- I did not understand a bit .... judging by the link ... user number 1 connects and writes a photo of an employee from his screw to the database ... user number 2 checks the link and the link that matches the location of the file on the server, issues the stream of this file and user number 2 sees the photo. - Rakzin Roman
- And it is not very clear where all the photos will be stored? you must then on the server's hard drive, and keep the link in the database. and what kind of "system" will this all be realized when I decided to add a photo, it should go to the server in the file system, and in the database, enter the link to the file ... and the reverse process ... can you please drop the link where is this done? or am I driving a nonsense ... and everything is done wrong? - Rakzin Roman 4:41 pm
I would advise you to refresh this memory
First, decide for yourself the question of what to store in the database - a link to the file or the file itself. (from my own experience I will say that both options have the right to life under certain external conditions).
If the solution is to store files in the database itself, then for this you need:
We read the byte array byte [] from the file (just the top link is useful if you have problems)
We write the resulting array in the desired CLOB field of the database table. How exactly this is done depends on your toolkit and habit.
Loading an image from the database is similar: from CLOB to byte [] and further depending on your needs (saving as a file or drawing)