简体   繁体   English

如何使用mongoose在MongoDB中的数据库中上传文件?

[英]How to upload file in database in MongoDB using mongoose?

I am using Node-Express and mongoose for database application. 我在数据库应用程序中使用Node-Express和mongoose。 I want to upload a file in database. 我想上传数据库中的文件。 So the clear idea is that when any user upload a photo or doc or any of the file it should be inserted into database the whole file. 因此,明确的想法是,当任何用户上传照片或文档或任何文件时,应将整个文件插入数据库。 I try to find on web a lot about this but it doesn't give me any proper answer. 我尝试在网上找到很多关于此的信息,但是它没有给我任何适当的答案。 Can you tell me the methods available in mongoose to achieve this? 您能告诉我猫鼬可用的方法吗?

Okay So you need to create an API which take the file and upload it to server. 好的,因此您需要创建一个API,该API可以接收文件并将其上传到服务器。

and you should upload that particular file to the public folder where you can serve that , 并且您应该将该特定文件上传到可以为该文件提供服务的公用文件夹中,

And in that API you should return the url of that particular file , and then you can save the url of that file in your db 并且在该API中,您应该返回该特定文件的url,然后可以将该文件的url保存在db

https://expressjs.com/en/starter/static-files.html https://expressjs.com/en/starter/static-files.html

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

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