Swagger NodeJS文件上传 [英] Swagger NodeJS File Upload

查看:79
本文介绍了Swagger NodeJS文件上传的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

解决方案:我的招摇合约出现错误,正确的文件上传定义应如下所示:

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

谢谢你的提示!

原始问题:

我的Swagger/NodeJS API有问题.

I have a problem regarding my Swagger/NodeJS API.

我想接收文件上传并将检索到的文件存储在另一个云服务中.

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

可悲的是,该文件未达到我期望的格式.这就是我通过邮递员在 req.swagger.params.image.value 中收到的信息:

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--

经过测试的图像作为PNG 或作为base64:

The tested image as a PNG or as base64:

iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==

我为图像提供的cloudprovider是cloudinary,因此当我使用以下代码时,它已成功保存到服务中.

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==")

当我使用

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

我希望文件将被转换为base64,但是我认为req中定义Content-Type等的部分也将被转换.

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.

有人知道一种处理文件上传和摇摇欲坠的方法,以便我可以将图像成功上传到cloudinary吗?

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

正则表达式似乎很"hacky" ...

Regex seems very "hacky"...

这是我招摇的合同中的imageupload部分:

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"

推荐答案

在我看来,您正在上传的是原始二进制图像,而不是base64编码的图像.我将废除所有二进制文件,仅在swagger文档中定义base64,并确保Buffer-> base64编码按预期工作(进行一些控制台日志记录).

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).

这篇关于Swagger NodeJS文件上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆