首页 文章

Masonite - AttributeError> 'str'对象没有属性'filename'

提问于
浏览
1

我有一个看起来像这样的表单:

<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'

1 回答

  • 1

    这是因为您的HTML表单上没有编码集:

    <form method="POST" action="/posts">
    

    这应该改为:

    <form method="POST" action="/posts" enctype="multipart/form-data">
    

    这将对图像进行编码,以便Masonite可以将其读作对象而不是字符串 .

相关问题