使用swagger操作上传多个文件

时间:2015-07-14 07:14:12

标签: 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"

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

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

<input class="parameter" type="file" name="file[]">

中缺少“倍数”一词

如果我编辑源代码并添加单词“multiple”,如下所示,我设法选择多个文件。

<input class="parameter" type="file" name="file[]" multiple>

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

对我有任何想法或建议吗?

谢谢。

2 个答案:

答案 0 :(得分:3)

答案 1 :(得分:0)

这已经在 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"

effect picture