John
John

Reputation: 103

How upload image with feathersjs?

I have a for User Signup. I'm doing this:

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

I am succeeded to add the user to the database. Now, I want to upload an image in the "image" folder and save its path in the attribute "Image". How can I do that?

Upvotes: 1

Views: 3469

Answers (1)

sha-1
sha-1

Reputation: 2093

There is a tutorial given in FeathersJS's official documentation to upload a file. You can follow this tutorial to upload your image file.

However, you can upload your image file through a separate request using the uploads service, shown in the tutorial, and as a response, you will receive the filename saved in the specified folder. Something like this:

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

Here, the "id" is the filename.

Then you can attach this file name to the user object as:

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

and save it using the users service or whatever you are already using to save the user.

Also, if you want to do it in a single request, then submit the file and user data to users service and in a before hook call the uploads service, save the file and attach this data in the user object, so that the users service can save it.

Also, remember to save your images in public/images folder so that they can be accessed like: your-domain.com/images/6454364d8facd7a88e627e9329ffc2b7a5c879dc838.jpeg.

Upvotes: 2

Related Questions