简体   繁体   中英

apache/python : file upload not working as expected

I am working on apache/python2.7 and trying a simple file upload.

html

<form enctype="multipart/form-data"  method="POST">
   <p>Upload File: <input type="file" name="file" id="InputFile"></p>
   <p><input type="submit"  name="ws_butt" value="Create Bulk" class="bulk_submit"></p>
</form>

python

request_body_size = int(environ.get('CONTENT_LENGTH', 0))
request_body = environ['wsgi.input'].read(request_body_size)
d = parse_qs(request_body)

If I print out the value of "d", I get as :

{' name': ['"file"', '"ws_butt"\r\n\r\nCreate Bulk\r\n------WebKitFormBoundaryCXKZ4XpBfD6P2BIu--\r\n'], ' filename': ['"s3essentials-jason.csv"\r\nContent-Type: text/csv\r\n\r\n"Time","Note"\r\n"0:11:47","allows multipart upload concurrently']}

I can not access "file" key and save the file somewhere and read the contents. How to do that?

Even if I try using FieldStorage, it is empty

form = cgi.FieldStorage(environ=environ, fp=environ['wsgi.input'])

How to deal with this? Am I missing some part?

Take a look at this tutorial .

So basically if you're using Flask its already taking care of the uploaded files. All you need to do is to save that files somewhere on your server:

if request.method == 'POST':
        file = request.files['file']
        if file:
                file.save('/where/to/save', file.filename)

I've stripped the tutorial example, to make it ease to see what's going on, but, please, refer to tutorial to see how to secure filename.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM