错误"AttributeError:'unicode'对象在文件上载时没有属性'read'"

ens*_*are 5 python pylons

我正在使用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'

这里发生了什么?谢谢你的帮助.

Fre*_*Foo 3

的两个参数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)

  • 在遵循拉斯曼的回答后遇到问题的原因是您实际上并未上传文件。这是因为 HTML 中的“enctype”参数出现在*表单*上,而不是字段上。 (2认同)