Reputation: 2043
i am trying to set up a possibility to upload xml files using mod_wsgi. to do so i build a formular like this one:
<form action="upload.py" method="post" enctype="multipart/form-data">
<input name="file" type="file" accept="text/xml"><br/>
<input type="submit" value="upload">
</form>
and a file upload.py
:
..
form = cgi.FieldStorage(fp=environ['wsgi.input'],
environ=environ,
keep_blank_values=True)
fileitem = form['file']
..
however, this gives me a keyerror while accessing 'file' in form. printing environ['wsgi.input'].read(request_body_size)
shows:
------WebKitFormBoundaryeplWkFZe2clFMtgf
Content-Disposition: form-data; name="file"; filename="file.xml"
Content-Type: application/octet-stream
<?xml version="1.0" ?>
....
as consequence, wsgi.input seems to be valid. debugging exposes that cgi.FieldStorage() = FieldStorage(None, None, [])
. have you an idea where the problem may be? am i missing something?
Upvotes: 0
Views: 1565
Reputation: 111
I noticed the same and placing the lines:
request_body_size = int( environ.get('CONTENT_LENGTH') )
print environ['wsgi.input'].read(request_body_size)
before calling the cgi.FieldStorage()
with wsgi.input
as an argument, it shows the body. Apparently that wsgi.input
fileIO-object gets consumed inside the FS and one cannot read it after that. I tried to read it twice before using it with FS and the same happens, only first works.
Edit: Yes, and it gets even worse, wsgi.input does not implement .seek() method to wind it back to beginning. Only reasonable way is to make a copy of it into io.BytesIO class instance and feed that into FS. It can be wound back with .seek() later, whatever. Note that one could use StringIO but that breaks if you're uploading binary files.
Upvotes: 1