question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Can't use FromFiles and FromForm in same request

See original GitHub issue

For some reason, I can’t seem to be able to perform both a multi-file upload with additional data on the same form.

Specs:

  1. Python 3.10.4 (inside virtual environment)
  2. blacksheep 1.2.5
  3. 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:closed
  • Created a year ago
  • Reactions:1
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
RobertoPrevatocommented, Apr 27, 2022

@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.

1reaction
nico-vromanscommented, Apr 26, 2022

@RobertoPrevato, awesome, thank you 👍🏻 🥳

Read more comments on GitHub >

github_iconTop Results From Across the Web

Blacksheep - can't use FromFiles and FromForm in same ...
For some reason, I can't seem to be able to perform both a multi-file upload with additional data on the same form.
Read more >
[FromForm] attribute - ASP.NET Core 5.0 Web API Tutorial
... try restarting your device. Your browser can't play this video. ... [ FromForm ] attribute: Bind the from-data | ASP.NET Core 5.0...
Read more >
Uploading files using 'fetch' and 'FormData' · Muffin Man
Today I learned: To upload files using fetch and FormData ... between fields we are sending in our request (similar to & for...
Read more >
Uploading Files — Flask Documentation (1.1.x)
The application accesses the file from the files dictionary on the request object. use the save() method of the file to save the...
Read more >
How to upload and download files PHP and MySQL
This tutorial demonstrates how you can upload files of various formats including .zip, .pdf, .docx, .ppt, as well as image files through a...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found