Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple File upload thought form-data #474

Open
relyativist opened this issue Mar 14, 2023 · 4 comments
Open

Multiple File upload thought form-data #474

relyativist opened this issue Mar 14, 2023 · 4 comments
Labels

Comments

@relyativist
Copy link

relyativist commented Mar 14, 2023

I'm currently using flask_smorest.fields.Upload to handle file uploads, but this only allows selecting a single file. Is it possible to upload many files?

I am trying to upload multiple zip archives serializing request with flask_smorest.fields.Upload, in resources im use blueprint arguments with @blp.arguments(UploadShema, location="files") and getting file with f = files["data"]. But the issue is f returns only first file added thought form-data.

How could this issue be resolved?

@lafrech
Copy link
Member

lafrech commented Mar 14, 2023

You can put as many fields as you want in the arguments schema (data_1, data_2,...).

class MultipartSchema(ma.Schema):
file_1 = Upload()
file_2 = Upload()
@blp.route("/", methods=["POST"])
@blp.arguments(MultipartSchema, location="files")
def func(files):
return {
"file_1": files["file_1"].read().decode(),
"file_2": files["file_2"].read().decode(),
}

@relyativist
Copy link
Author

relyativist commented Mar 14, 2023

@lafrech Yes, and I am using it for another file field in form-data. However, what if I have 10-20 zip archives and I want to serialize them in list-type instance file_1 = Upload() . What if I am not assume how much files will be uploaded. And want to have a list of all uploaded files in one row of upload filed of form-data
image.

Somthing like this file_1 = fields.List(Upload()) @blp.arguments(MultipartSchema(many=True), location="files") .

@alwin48
Copy link

alwin48 commented Oct 25, 2023

I think you can use:

class MultipartFileSchema(Schema):
    file_list = fields.List(Upload())

and

@blp.arguments(MultipartSchema(many=True), location="files")
def func(response): 
    files = response['file_list']
    for file in files:
        any operation

@zhangwenting0224
Copy link

zhangwenting0224 commented May 6, 2024

@alwin48 The method mentioned is a feasible one, but here's a method that's closer to what we actually want. Here's how I did it, sharing it for everyone's reference.

from marshmallow_dataclass import class_schema    
from flask_smorest.fields import Upload
from marshmallow import fields

@dataclass
class MultipartFile(BaseSchema):
    files: List[FileStorage] = field(metadata={"marshmallow_field": fields.List(Upload())})
MultipartFileSchema = class_schema(MultipartFile)

e.g
@blp.arguments(MultipartFileSchema, location="files")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants