Swagger NodeJS 文件上传文件上传、Swagger、NodeJS

2023-09-07 11:08:36 作者:越努力ヾ越幸运

解决方案:在我的招摇合同中有一个错误,文件上传的正确定义应该是这样的:

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

我的图像云提供者是 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"...

这是我招摇合同中的图片上传部分:

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