简体   繁体   English

apache / python:文件上传未按预期工作

[英]apache/python : file upload not working as expected

I am working on apache/python2.7 and trying a simple file upload. 我正在使用apache / python2.7并尝试简单的文件上传。

html 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 : 如果我打印出“ d”的值,我将得到:

{' 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 即使我尝试使用FieldStorage,它也为空

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. 因此,基本上,如果您使用的是Flask,它已经在处理上传的文件。 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. 我已经删除了本教程的示例,以便于查看正在发生的事情,但是请参考该教程以了解如何保护文件名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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