有没有办法实现在MERN中从服务器上载文件并查看文件的方法?

时间:2019-05-28 07:58:27

标签: node.js reactjs mongodb express

我想为用户实现一项功能,以上传文件(就像docs,ppts,文本文件一样)并保存到数据库,而另一个则可以查看从服务器获取的内容? 是否有任何框架或npm模块?

1 个答案:

答案 0 :(得分:0)

Yes. You can have one api endpoint to send files from your react app in the form of FormData and recieve/handle it in your express app using Multer. Then you can have another api endpoint to send file from your server and you can send the file using res.sendFile(). In your react app you need to access the file convert it into blob and then into dataUrl and then you can view it easily using that dataUrl.