【问题标题】:simple way to upload image in mongo db using sails js使用sails js在mongo db中上传图像的简单方法
【发布时间】:2020-02-04 10:54:34
【问题描述】:

我想在我的 Sails 应用程序中上传一张图片。如何定义模型以及如何将图像存储到数据库以及如何下载它。任何人都可以请帮忙。 db:mongo。

module.exports = {

  attributes: {

  employeeId:{
      type: 'string'
    },
 addressproof:{
      type:______________
},

我应该如何声明地址证明的类型。我希望用户也上传地址证明图像。在控制器中我应该写什么以便可以存储和检索地址证明。

【问题讨论】:

标签: mongodb sails.js


【解决方案1】:

you can use gridFS mongo 用于将文件保存到 mongodb 中。

在此之前你必须得到你的文件:


req.file('yourFileName').upload({
maxBytes: 10000000 },(err,uploadedFiles)=>{
if(err)
  {
  // res your error
  } 
    const file = uploadedFiles.map(item => fs.createReadStream(item.fd))
 }
);

然后您可以保存上面代码中提到的 const 文件并使用 gridFS 保存。

不要忘记您需要在代码开头添加 :const fs = require('file-system')

【讨论】:

  • 你拒绝了编辑答案,把它添加为你自己的东西,很糟糕
  • @DulajKulathunga 女巫编辑答案。我没有编辑其他答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-19
  • 1970-01-01
  • 2012-09-26
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多