我有一个看起来像这样的表格:
<form method="POST" action="/posts">
{{ csrf_field }}
<input type="text" name="username">
<input type="file" name="image">
<input type="submit" value="Submit">
</form>
但是当我提交此表单并尝试上传时,我只会得到图像的名称:
def posts(self, request: Request, upload: Upload):
upload.store(request().input('image'))
我被打中有一个例外:
AttributeError > 'str' object has no attribute 'filename'
答案 0 :(得分:2)
之所以抛出该错误,是因为您的HTML表单上没有设置编码:
<form method="POST" action="/posts">
应更改为:
<form method="POST" action="/posts" enctype="multipart/form-data">
这将对图像进行编码,以便Masonite可以将其读取为对象而不是字符串。