使用swagger操作上传多个文件

cri*_*spy 3 rest python-2.7 swagger swagger-ui multiple-file-upload

我目前正在使用 swagger 操作进行多个文件上传。以下是我正在使用的代码:

class uploadImage(Resource):

  @swagger.operation(

        notes='Upload an image file',

        parameters=[
          {
            "name": "file[]",
            "description": "Upload an image file. File size limit is 3MB. Only '.jpg' is allowed ",
            "required": True,
            "allowMultiple": True,
            "dataType": 'file',
            "paramType": "form"
          }
])

def post(self):
    files=request.files.getlist['file[]']
    filenames = []
    for file in files:
        filename = secure_filename(file.filename)
        filenames.append(filename)
        print "Files are uploaded successfully"
Run Code Online (Sandbox Code Playgroud)

虽然我在代码中插入了“allowMultiple”:True,但它没有显示在swagger UI中。服务器启动后,我尝试查看html源代码,“多个”不会显示在表单中。

以下是服务器启动时swagger ui的源代码:

<input class="parameter" type="file" name="file[]">
Run Code Online (Sandbox Code Playgroud)

中缺少“多个”一词。

如果我编辑源代码并添加“多个”一词,如下所示,我可以选择多个文件。

<input class="parameter" type="file" name="file[]" multiple>
Run Code Online (Sandbox Code Playgroud)

在这种情况下, “allowMultiple”:True对我来说不起作用。

对我有什么想法或建议吗?

谢谢。

小智 5

OpenAPI 3.0 中的 swagger 已经支持这一点。请参阅https://swagger.io/docs/specification/describing-request-body/file-upload/

例子:

  /schema:
    get:
      tags:
        - Schema
      description: download schema file
      responses:
        "200":
          description: success
          content:
            multipart/form-data:
              schema:
                 type: object
                 properties:
                   filename:
                     type: array
                     items:
                       type: string
                       format: binary
        "400":
          $ref: "#/components/responses/failed"
        "404":
          $ref: "#/components/responses/notExist"
Run Code Online (Sandbox Code Playgroud)

效果图