因此,我试图制作一个用于上传个人资料图像的表格,但是当我尝试上传图像时,我一直收到此错误AttributeError: 'FileField' object has no attribute 'save'
。
我的观点:
change_info_form = ChangeInformationForm()
if change_info_form.validate_on_submit():
if change_info_form.img.data != None:
path = str(app.config['UPLOAD_FOLDER']) + str(current_user.profile_img)
if os.path.exists(path) and str(current_user.profile_img) != 'default.svg':
os.remove(path)
change_info_form.img.save(os.path.join(app.config['UPLOAD_FOLDER'], str(current_user.id)))
我的表单:
class ChangeInformationForm(FlaskForm):
img = FileField()
我的HTML:
<img id="img-image" src="{{ url_for('static', filename='uploads/{}'.format(current_user.profile_img)) }}" style="width: 2.5em; height: 2.5em;border-radius: 50%; margin-right: 1em;" />
<label for="img_input" class="img-input-lable">Select file</label>
{{ change_info_form.img(class='input-ctl-img', id='img_input', type='file') }}
我尝试过的事情:
change_info_form.img.save(os.path.join(app.config['UPLOAD_FOLDER'], str(current_user.id)))
change_info_form.img.file.save(os.path.join(app.config['UPLOAD_FOLDER'], str(current_user.id)))
change_info_form.img.data.save(os.path.join(app.config['UPLOAD_FOLDER'], str(current_user.id)))
我到处搜索,但无法理解该错误。
谢谢。