2

I'm using Loopback4 to create a sort-of blog api. My posts model will contain an image (or perhaps several in the future).

What i'm trying to do is -> upload image when creating a new post.

I've read quite a few articles regarding this, but since i'm new to nodejs and loopback4 i'm having a bit of trouble making it work.

I've followed the answer written here Loopback 4: Upload multipart/form-data via POST method and i'm encountering some problems with it.

First: I'm using MySQL. Is there any way to save the image in the database? Or is it not a good practice? The work-around would be to upload the image and save only the image location in the DB.

Second: After following the tutorial and ending up with the code created i made a new post request for testing purposes. It looks something like this.

  @post('/posts/upload', {
    responses: {
      '200': {
        description: 'Post model instance',
        content: { 'application/json': { schema: { type: 'object' } } },
      },
    },
  })
  async uploadFile(@requestBody({
    description: 'multipart/form-data value.',
    required: true,
    content: {
      [FORM_DATA]: {
        schema: { 'media-type': Post },
      },
    },
  })
  body: unknown,
  ) {
    return body;
  }

That creates my post request in Swagger, but it shows like a big input box (text input). As far as i know swagger supports upload button. Is the content-type wrong? How could i test my upload function? I did something similar in NetCore2 and i had to convert my image to bytes (if i remember correctly), is the same problem here?

Any tips? Thanks!

Dante R.
  • 902
  • 3
  • 13
  • 41

0 Answers0