【问题标题】:Multiple files upload using swagger operation使用swagger操作上传多个文件
【发布时间】:2015-07-14 07:14:12
【问题描述】:

我目前正在使用 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[]">

“多个”这个词在 .

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

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

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

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

谢谢。

【问题讨论】:

    标签: rest python-2.7 swagger swagger-ui multiple-file-upload


    【解决方案1】:

    这只是 Swagger 不支持的。见https://github.com/swagger-api/swagger-spec/issues/254

    【讨论】:

    • 谢谢@Ron。我改变了方法。只允许一个文件。
    • 更新:OpenAPI 3.0 支持描述包含文件数组的多部分请求。这可以在 Swagger UI 3.26.0+ 中进行测试。
    【解决方案2】:

    这已经被 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

    【讨论】:

    • 这不适用于流式文件上传
    猜你喜欢
    • 1970-01-01
    • 2015-12-22
    • 1970-01-01
    • 2012-03-05
    • 2016-12-26
    • 1970-01-01
    • 2015-10-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多