修复文章编辑表单无法提交的问题

- 移除嵌套的附件上传form(HTML不允许表单嵌套)
- 改用div和JavaScript FormData处理附件上传
This commit is contained in:
2026-06-04 23:26:50 +08:00
parent 8e3aeaf103
commit f3ccf25ede

View File

@@ -387,6 +387,39 @@ editor.addEventListener('input', updatePreview);
// 初始化预览
updatePreview();
// 附件上传函数
{% if article %}
function uploadAttachment() {
const formData = new FormData();
formData.append('article_id', document.getElementById('attachment-article-id').value);
formData.append('file', document.getElementById('attachment-upload-file').files[0]);
formData.append('description', document.getElementById('attachment-desc').value);
fetch('{{ url_for("admin.upload_attachment") }}', {
method: 'POST',
body: formData
})
.then(res => res.json())
.then(data => {
if (data.success) {
location.reload();
}
});
}
function deleteAttachment(id) {
if (confirm('确定删除此附件?')) {
fetch(`/admin/attachment/${id}/delete`, {method: 'POST'})
.then(res => res.json())
.then(data => {
if (data.success) {
location.reload();
}
});
}
}
{% endif %}
</script>
{% endblock %}
@@ -515,43 +548,13 @@ updatePreview();
</div>
{% endfor %}
</div>
<form id="attachment-upload-form" enctype="multipart/form-data">
<input type="hidden" name="article_id" value="{{ article.id }}">
<input type="file" name="file" id="attachment-upload">
<input type="text" name="description" placeholder="文件描述(可选)" style="display: inline-block; width: 200px;">
<div class="attachment-upload-area">
<input type="hidden" id="attachment-article-id" value="{{ article.id }}">
<input type="file" id="attachment-upload-file">
<input type="text" id="attachment-desc" placeholder="文件描述(可选)" style="display: inline-block; width: 200px;">
<button type="button" onclick="uploadAttachment()" class="btn btn-sm btn-secondary">上传附件</button>
</form>
</div>
</div>
<script>
function uploadAttachment() {
const form = document.getElementById('attachment-upload-form');
const formData = new FormData(form);
fetch('{{ url_for("admin.upload_attachment") }}', {
method: 'POST',
body: formData
})
.then(res => res.json())
.then(data => {
if (data.success) {
location.reload();
}
});
}
function deleteAttachment(id) {
if (confirm('确定删除此附件?')) {
fetch(`/admin/attachment/${id}/delete`, {method: 'POST'})
.then(res => res.json())
.then(data => {
if (data.success) {
location.reload();
}
});
}
}
</script>
{% endif %}
<div class="form-group">