修复文章编辑表单无法提交的问题
- 移除嵌套的附件上传form(HTML不允许表单嵌套) - 改用div和JavaScript FormData处理附件上传
This commit is contained in:
@@ -387,6 +387,39 @@ editor.addEventListener('input', updatePreview);
|
|||||||
|
|
||||||
// 初始化预览
|
// 初始化预览
|
||||||
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>
|
</script>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
||||||
@@ -515,43 +548,13 @@ updatePreview();
|
|||||||
</div>
|
</div>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</div>
|
</div>
|
||||||
<form id="attachment-upload-form" enctype="multipart/form-data">
|
<div class="attachment-upload-area">
|
||||||
<input type="hidden" name="article_id" value="{{ article.id }}">
|
<input type="hidden" id="attachment-article-id" value="{{ article.id }}">
|
||||||
<input type="file" name="file" id="attachment-upload">
|
<input type="file" id="attachment-upload-file">
|
||||||
<input type="text" name="description" placeholder="文件描述(可选)" style="display: inline-block; width: 200px;">
|
<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>
|
<button type="button" onclick="uploadAttachment()" class="btn btn-sm btn-secondary">上传附件</button>
|
||||||
</form>
|
</div>
|
||||||
</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 %}
|
{% endif %}
|
||||||
|
|
||||||
<div class="form-group">
|
<div class="form-group">
|
||||||
|
|||||||
Reference in New Issue
Block a user