How upload image with feathersjs?

。_饼干妹妹 提交于 2019-12-12 10:20:58

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/47174807/how-upload-image-with-feathersjs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!