简体   繁体   English

如何通过将图像存储到数据库中来在 React js 中上传和显示图像

[英]How to upload and display images in react js by storing them into a database

I am have trouble uploading and display images.我无法上传和显示图像。 I can not find anyone who does it, can anyone help?我找不到任何人这样做,有人可以帮忙吗?

We cannot directly upload files to sql database rather we choose different ways to upload images and store the path of that image to sql, below I am going to mention the ways that could be helpful for you for uploading stuff,我们不能直接将文件上传到 sql 数据库,而是选择不同的方式上传图像并将该图像的路径存储到 sql,下面我将提到可能对您上传内容有帮助的方式,

  1. You can upload the images to a folder inside your client side project and store the path of that image which will be something like, localhost:3000/uploads/image.png, and you can change this path from local to your real server when you will actually upload your project to some server.您可以将图像上传到客户端项目中的文件夹,并存储该图像的路径,类似于 localhost:3000/uploads/image.png,并且您可以在使用时将此路径从本地更改为真实服务器实际上会将您的项目上传到某个服务器。 If you are just doing this in you laptop for practicing you can give path like D://images/image.png, and for getting images you can just use the path and you will receive the image.如果您只是在笔记本电脑上执行此操作以进行练习,则可以提供 D://images/image.png 之类的路径,而要获取图像,您只需使用该路径即可收到图像。

  2. You can use some cloud storage like aws s3 bucket and directly upload the images there and give the image path in database table same as in the above procedure.您可以使用诸如 aws s3 存储桶之类的云存储并直接将图像上传到那里,并在数据库表中提供与上述过程相同的图像路径。

Uploading images to a server is better approach in above point 1 we are upload on local server and second point we are upload aws server but both are servers and in database we are just storing the paths of that uploaded image.在上面的第 1 点中,将图像上传到服务器是更好的方法,我们在本地服务器上上传,第二点我们在 aws 服务器上上传,但两者都是服务器,在数据库中,我们只是存储上传图像的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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