You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用@SWG\Parameter实现多文件上传?Swagger接口文档求助

How to Implement Multi-File Upload with a Single @SWG\Parameter Annotation

Got it, let's fix your multi-file upload setup in Swagger! Your current configuration is close, but you're missing a couple of key pieces to tell Swagger this is an array of files instead of a single file.

Working Configuration Example

Here's the correct @SWG\Parameter block to enable multi-file upload:

@SWG\Parameter(
    description="Multiple media files to upload (use the array syntax to accept multiple files)",
    in="formData",
    name="vMediaName[]",
    required=true,
    type="array",
    collectionFormat="multi",
    @SWG\Items(
        type="file",
        format="file"
    )
)

Breakdown of Key Changes

Let's walk through why this works:

  • type="array": This explicitly tells Swagger that the parameter accepts a collection of values, not just one.
  • collectionFormat="multi": For formData parameters, this setting means the same parameter name (vMediaName[]) will be repeated for each file in the upload. This matches how browsers send multiple files in a form.
  • @SWG\Items(type="file"): Defines that each element in the array is a file type, which triggers the multi-file upload UI in Swagger UI.

Critical Additional Step: Set the Correct Content Type

Don't forget to add the consumes property to your API endpoint annotation (like @SWG\Post) to ensure Swagger knows the request uses multipart form data:

@SWG\Post(
    path="/upload-media",
    consumes={"multipart/form-data"},
    // ... other properties like summary, responses, etc.
    @SWG\Parameter(
        // the parameter block from above
    )
)

Backend Handling Note

On your backend (e.g., PHP), you'll access the files via the array syntax:

$uploadedFiles = $_FILES['vMediaName'];
// Loop through $uploadedFiles to process each file individually

Why Your Original Configuration Didn't Work

Your initial code set type="file" directly, which tells Swagger to expect a single file. By switching to type="array" and defining the item type as file, you enable the multi-file upload functionality properly.

内容的提问来源于stack exchange,提问作者Yagnesh Panchal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 07:55:33