Can't use FromFiles and FromForm in same request
See original GitHub issueFor some reason, I can’t seem to be able to perform both a multi-file upload with additional data on the same form.
Specs:
- Python 3.10.4 (inside virtual environment)
- blacksheep 1.2.5
- Ubuntu 20.04
Here is my html code (simplified):
<form action='/upload_files_with_additional_data' method="post" enctype='multipart/form-data'>
<input type="file" name="files" multiple>
<label for="checkbox1" class="checkbox">Checkbox 1</label>
<input type="checkbox" name="checkbox1">
<label for="checkbox2" class="checkbox">Checkbox 2</label>
<input type="checkbox" name="checkbox2">
<label for="textfield">Text field</label>
<input type="text" id="textfield" name="textfield">
<input type="submit" value="Submit">
</form>
Here’s what I’ve tried (but didn’t work) and what I expect would work:
@post('/upload_files_with_additional_data')
async def upload_files_with_additional_data(self, files: FromFiles, form_data: FromForm):
for file in Files:
# do something with each file
# do something with additional form data
I’ve also tried using FromForm[FormData]
with the following FormData
:
class FormData:
checkbox1: bool
checkbo2: bool
files: list # also tried with type hint FromFIles (and without setting files alltogether
# def __init__(self, checkbox1: str, checkbox2: str, files): # also tried with files: FromFiles and without entire __init__ (but that didn't work at all)
# self.checkbox1 = bool(checkbox1)
# self.checkbox2 = bool(checkbox2)
# self.files = files # also tried with FromFiles(files)
Either I can upload files, without sending additional form data (using only files: FromFiles
), or I can use the form data without the files (using only form: FromForm
). Combining the two doesn’t work.
When using async def upload_files_with_additional_data(self, request: Request):
I can get also get the form data from there using form = await request.form()
, but that only works for additional data, not the files. When I try to upload files I get:
form = await request.form()
File "blacksheep/messages.pyx", line 172, in form
File "blacksheep/contents.pyx", line 163, in blacksheep.contents.multiparts_to_dictionary
AttributeError: 'bytes' object has no attribute 'append'
I’ve also tried using async def upload_files_with_additional_data(self, bytes: FromBytes):
which does seem to contain all the data, but as bytes, and I’ve tried a few ways to convert these to what I want, but no success there either.
Is there a way of uploading both multiple files, alongside other form data (I’m fairly confident there is, but I just haven’t figured it out)? If so: how should this be achieved?
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:6 (3 by maintainers)
@nico-vromans thank you for reporting this bug! 🎉 I’m still looking into a couple of things, then I’ll publish a new version to PyPi with this fix. I have everything automated so I can release at any time to PyPi without issues.
@RobertoPrevato, awesome, thank you 👍🏻 🥳