我正在使用Pylons上传图像并将其存储到磁盘:
<form method="post">
<input type="file" name="picture" enctype="multipart/form-data" />
</form>
Run Code Online (Sandbox Code Playgroud)
然后在我的控制器中:
if 'picture' in request.POST:
i = ImageHandler()
#Returns full path of image file
picture_file = i.makePath()
shutil.copyfileobj(request.POST['picture'],picture_file)
Run Code Online (Sandbox Code Playgroud)
但是我收到错误:AttributeError:'unicode'对象没有属性'read'
这里发生了什么?谢谢你的帮助.
的两个参数copyfileobj现在都是字符串,而该函数将文件(或“类文件对象”)作为参数。做类似的事情
picture_file = open(i.makePath(), 'w')
Run Code Online (Sandbox Code Playgroud)
(或者只是picture_file = i不确定你的ImageHandler班级是什么样的),然后
shutil.copyfileobj(request.POST['picture'].file, picture_file)
Run Code Online (Sandbox Code Playgroud)