gpt4 book ai didi

python - 错误 "AttributeError: ' unicode' object has no attribute 'read' "on file upload

转载 作者:太空狗 更新时间:2023-10-30 01:24:17 25 4
gpt4 key购买 nike

我正在使用 Pylons 上传图像并将其存储到磁盘:

 <form method="post">
<input type="file" name="picture" enctype="multipart/form-data" />
</form>

然后在我的 Controller 中:

 if 'picture' in request.POST:

i = ImageHandler()

#Returns full path of image file
picture_file = i.makePath()

shutil.copyfileobj(request.POST['picture'],picture_file)

但是我收到错误:AttributeError: 'unicode' 对象没有属性 'read'

这是怎么回事?感谢您的帮助。

最佳答案

copyfileobj 的两个参数现在都是字符串,而该函数将文件(或“类文件对象”)作为参数。做类似的事情

 picture_file = open(i.makePath(), 'w')

(或者只是 picture_file = i,不确定你的 ImageHandler 类是什么样的),然后

 shutil.copyfileobj(request.POST['picture'].file, picture_file)

关于python - 错误 "AttributeError: ' unicode' object has no attribute 'read' "on file upload,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3907832/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com