【问题标题】:Uploading image to nodejs api to store in mongoDB将图像上传到 nodejs api 以存储在 mongoDB
【发布时间】:2021-03-11 22:53:26
【问题描述】:

我有一个 mongoDB 的用户模型。下面是模型。如您所见,有一个缓冲区类型,我必须在其中存储图像。如何编写路线以添加具有以下所有详细信息(包括图像)的用户。图像不会高于 5mb。我还需要知道如何在 post man 中测试它。

const userSchema = new db.mongoose.Schema ({
  name  : String,
  phoneNumber : String,
  Email : String,
  password  : String,
  CreatedDate : {type: Date, default : Date.now()},
  Proof       : Buffer,
  TotalOrder  : {type : Number, default: 0},
  TotalSales  : {type : Number, default: 0},
  isAdmin     : { type  : Boolean, default  : false },
});

【问题讨论】:

  • 详细说明存储和测试图像的整个方法在 StackOverflow 答案中是不可取的。如果您提及您的尝试和无效,您更有可能获得帮助。

标签: node.js mongodb mongoose postman


【解决方案1】:

Stackoverflow 不是你可以指望别人做你的工作的地方。所以,在这里我只是指出(不限于)上传文件所需的内容:

  1. multer,一个node.js中间件,你可以学习here如何使用multer上传图片
  2. 您需要fs 才能访问文件系统。阅读文档。

如果您知道如何使用 (Express) 之类的任何库设计 API,那么通过使用上述两种技术,您可能会成功。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-09-13
    • 2017-10-22
    • 1970-01-01
    • 2019-06-12
    • 1970-01-01
    • 2020-11-26
    • 1970-01-01
    • 2017-09-25
    相关资源
    最近更新 更多