且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将图像添加到类并保存在数据库中

更新时间:2023-02-15 11:14:43

您不能直接将图像保存在类对象和数据库中.为此,您可以通过两种方式做到这一点:

You can not directly save the image in the class object and in the database. To achieve this you can do this in two ways:

  1. 您从请求中获取图像,将其保存到服务器,然后将该特定文件的路径保存在数据库中.这是实现此目标的***方法,因为它不会对您的数据库大小产生任何影响.

  1. You get the image from the request, save it to your server, and save the path of that particular file in the database. This is the best way to achieve this as it will not hit any impact on your database size.

如果仅要将文件保存在数据库中,则可以将数据库列数据类型用作BLOB,这允许您保存文件或冗长的对象.在课堂上,您将制作照片道具.作为字符串并保存图像或文件的base64(推荐),或将该属性设置为Stream并将文件流保存在此特定对象中,并以相同的方式推送到数据库.

If you want to save the file in the database only then you can use the database column datatype as BLOB which allows you to save the file or lengthy object. In your class, you make the photo prop. as the string and save the base64 of the image or file (recommended) or make the property as Stream and save the file stream in this particular object and same push to database.