Swagger NodeJS File Upload

家住魔仙堡 提交于 2021-01-04 06:44:32

问题


Solution: Had an error in my swagger contract, correct definition of a fileupload should look like this:

  parameters:
   - in: formData
     name: file
     description: The file to upload
     required: true
     type: file

Thank you for the hint!

Original question:

I have a problem regarding my Swagger/NodeJS API.

I want to receive fileuploads and store the retrieved file with another cloud service.

Sadly, the file does not arrive in the format I expect it to be. This is what I receive in req.swagger.params.image.value via postman:

    ------WebKitFormBoundaryZCFoUQnf4lHIhzjj
Content-Disposition: form-data; name="image"; filename="r14kvzvmsh3xnuyl2vq8.png"
Content-Type: image/png

�PNG

IHD�o&�IDA�c���?� � ��1��X��5�юIEND�B`�
------WebKitFormBoundaryZCFoUQnf4lHIhzjj--

The tested image as a PNG or as base64:

iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==

My cloudprovider for the images is cloudinary, so when I use the following code it is successfully saved to the service.

cloudinary.v2.uploader.upload("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==")

When I use

new Buffer(req.swagger.params.image.value, 'binary').toString('base64');

I expected the file to be converted to base64, but I think the part in the req defining the Content-Type etc. is also converted.

Does anybody know a way to work with fileuploads and swagger, so I can upload the image successfully to cloudinary?

Regex seems very "hacky"...

This is the imageupload part in my swagger contract:

/imageUpload:
x-swagger-router-controller: image_upload
post:
  description: Endpoint to upload the image file. The image has to be uploaded before the shipment is created, a response will be the image ID that has to be supplied to the shipment. If the image ID is not valid, i.e. not known to this api, the shipment can not be created
  operationId: storeImage
  consumes:
    - multipart/form-data
    - application/x-www-form-urlencoded
    - binary
  parameters:
   - in: body
     name: image
     description: The file to upload
     required: true
     schema:
        type: string
        format: file
  responses:
    "200":
      description: Success
      schema:
        # a pointer to a definition
        $ref: "#/definitions/ImageResponse"
    # responses may fall through to errors
    default:
      description: Error
      schema:
        $ref: "#/definitions/ErrorResponse"

回答1:


Looks to me like you're uploading the raw binary image instead of the base64 encoded image. I'd do away with all the binary stuff, define only base64 in your swagger doc, and ensure the Buffer->base64 encoding is working as expected (do some console logging).



来源:https://stackoverflow.com/questions/39043154/swagger-nodejs-file-upload

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