简体   繁体   中英

Storing image in database in asp.net core. Should I store the image in bytes of just stooring the path of uploaded file is enough

I am storing the image file details in the database. Along with the path, I am also planning to store the byte array in case of accidental deletion of the uploaded folder. I would be using SQL express database as I want to use the app locally. As the size of SQL express is 10GB is it advisable to store a byte array of images in the database. Does the size of the byte array is same as the size of the image? As I am expecting the images to be of around 5-10MB.

using (var fileStream = new FileStream(filePath, FileMode.Create))
                    {
                        await postPatientInformationEntity.PatientPhoto.CopyToAsync(fileStream);
                        fileInformationEntity.Path = filePath;
                        fileInformationEntity.Name = postPatientInformationEntity.PatientPhoto.FileName;
                        fileInformationEntity.Type = postPatientInformationEntity.PatientPhoto.ContentType;

                    }
                    using (var ms = new MemoryStream())
                    {
                        await postPatientInformationEntity.PatientPhoto.CopyToAsync(ms);
                        var fileBytes = ms.ToArray();
                        fileInformationEntity.FileData = fileBytes;
                        // act on the Base64 data
                    }

An alternative, and better method is to store the images outside of the database and store only a link to the image file. You only need a text field in your database table to store this information. The only problem to this approach is that you must synchronize the data in the link field with your file system. The possible solution is to store it in the file system, if the images need to be accessed from another server, tools like rsync can be used to move files from one server to other. this tools can be automated in order to sync the filesystems in constant intervals. The path of uploaded file is enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM