【发布时间】:2017-01-17 12:14:37
【问题描述】:
我已经在这里看到了描述相同问题的类似问题,人们在那里给出了答案,甚至有人回应它有所帮助,但它对我没有任何作用。
这是我的代码:
<html>
<body>
<form action = "/anomalydetector" enctype = "multipart-form-data" method = "post">
File: <input name = "attachment" type = "file" /><br />
Analyzer sensitivity in percents: <input type = "text" name = "sensitivity" value = "10" /><br />
<input type = "submit" value = "Analyze" />
</form>
</body>
</html>
和处理程序:
class AnomalyDetectorPage(webapp2.RequestHandler):
def post(self):
uploaded_file = self.request.POST.get("attachment");
file_data = uploaded_file.file.read();
我总是遇到这种错误:
File "/base/data/home/apps/s~test-ml/1.398533980585659886/main.py", line 207, in post
file_data = uploaded_file.file.read();
AttributeError: 'unicode' object has no attribute 'file'
我知道python认为文件是字符串,但我能用它做什么???
我尝试了self.request.POST.get("attachment").file.read()、self.request.POST["attachment"].file.read()、self.request.get("attachment").file.read() 和self.request.POST.multi["attachment"].file.read(),也许还有其他方法,但我总是收到此错误。
如何读取此文件的内容?
【问题讨论】:
-
根据错误信息
uploaded_file是一个Unicode对象。打印出来是什么样子的? -
@PM2Ring - 就像放在字段中的文件名。只是文件名,没有路径。
-
@Kosmos 为什么您希望文件名具有
file属性? -
@Goyo - 我希望它不是文件名,而是
cgi.FieldStorage对象 -
@Kosmos 愚蠢的问题,我看错了之前的 cmets。
标签: python file-upload webapp2