如何用feathersjs上传图片?

How upload image with feathersjs?

我有一个用户注册。我这样做:

 app.service('users').create(
   {
      email:  this.state.Username,
      Nom: this.state.Name,
      Image: "../image/p.png",
   })

我成功将用户添加到数据库中。现在,我想上传 "image" 文件夹中的图像并将其路径保存在属性 "Image" 中。我该怎么做?

有一个tutorial given in FeathersJS's official documentation to upload a file。您可以按照本教程上传图像文件。

但是,您可以使用教程中显示的 uploads 服务通过单独的请求上传图像文件,作为响应,您将收到保存在指定的文件夹。像这样:

{
   "id":"6454364d8facd7a88e627e9329ffc2b7a5c879dc838.jpeg",
   "uri": "the-same-uri-we-uploaded",
   "size": 1156
}

这里的"id"是文件名。

然后您可以将此文件名附加到用户对象,如:

{
   email:  this.state.Username,
   Nom: this.state.Name,
   Image: "6454364d8facd7a88e627e9329ffc2b7a5c879dc838.jpeg",
}

并使用 users 服务或您已经使用的任何用于保存用户的服务来保存它。

此外,如果您想在单个请求中执行此操作,请将文件和用户数据提交给 users 服务,然后在 before hook 中调用 uploads 服务,保存文件并将此数据附加到用户对象中,以便users 服务可以保存它。

此外,请记住将您的图像保存在 public/images 文件夹中,以便可以通过以下方式访问它们:您的域。[=44= .jpeg.