klein icon indicating copy to clipboard operation
klein copied to clipboard

Handle uploading multiple files

Open notoriousno opened this issue 8 years ago • 1 comments

Tornado, Flask, Django, and most current web frameworks allows for convenient functions for uploading files to the backend. Klein should handle file uploads in a similar fashion as well. Here's a quick example of how it could work:

@route('/')
def example(request):
    # example Request.files
    file_0 = request.files['my_files'][0]
    file_1 = request.files['my_files'][1]

    # example of what a "HTTP File" object would contain
    file_0.filename  # str
    file_0.content    # BytesIO

This issue cannot be addressed without bringing up the infamous Issue 288. There are some great ideas in the comments of that issue. One comment suggests processing args after the request object has been created. Building upon that, we should be able to implement a solution for files (at one point when tx.web2 was a thing, it seems that this was a possible). This would involve extending web.Request or creating a Klein specific Request component. I'm just throwing this idea up here in case anyone has suggestions or has anything.

As a contingency plan, another utility function could be created and it could replicate, for instance, what Tornado does for files.

from klein.utils import fileinfo
my_file = fileinfo(request.args[b'my_file'][0])
print( my_file.filename )
print( my_file.content )

References

  • https://stackoverflow.com/questions/17350733/cant-get-uploaded-file-in-twisted
  • https://stackoverflow.com/questions/47440098/twisted-upload-muliple-files-key-error

notoriousno avatar Nov 25 '17 16:11 notoriousno

@notoriousno

its too bad that this hasn't been addressed, it seems like such a basic function that twisted should be capable of.

@wsanchez do you know if there is any progress on this on the twisted side? I know web2 was around but that seems like ancient history now.

One idea would be to provide a callback after header has been downloaded. This would allow twisted web to offer different "paths" than just the Request branch.

SoundsSerious avatar Aug 23 '22 03:08 SoundsSerious