增加文件系统存储功能,修复上传功能
Change-Id: I7b76c0a78fa0a02018fa76e932d283e29b35d4be
diff --git a/Merge/front/src/components/CreatePost.jsx b/Merge/front/src/components/CreatePost.jsx
index c11e247..2b365db 100644
--- a/Merge/front/src/components/CreatePost.jsx
+++ b/Merge/front/src/components/CreatePost.jsx
@@ -51,12 +51,12 @@
.catch(err => setError(err.message))
.finally(() => setLoading(false))
}, [isEdit, postId])
- // 上传回调
+
+ // 上传回调 - 保存原始文件对象
const handleUploadComplete = async uploadedFiles => {
- // TODO: 真正上传到服务器后替换为服务端 URL
- const urls = await Promise.all(
- uploadedFiles.map(f => URL.createObjectURL(f))
- )
+ setFiles(uploadedFiles)
+ // 为预览创建临时URLs
+ const urls = uploadedFiles.map(f => URL.createObjectURL(f))
setMediaUrls(urls)
setStep('detail')
}
@@ -72,25 +72,42 @@
return
}
setError(null)
+
try {
+ // 创建FormData对象
+ const formData = new FormData()
+
+ // 添加文本字段
+ formData.append('user_id', currentUserId)
+ formData.append('title', title.trim())
+ formData.append('content', content.trim())
+ formData.append('status', status)
+ if (topicId) {
+ formData.append('topic_id', topicId)
+ }
+
if (isEdit) {
- await updatePost(postId, {
- title: title.trim(),
- content: content.trim(),
- topic_id: topicId || undefined,
- media_urls: mediaUrls,
- status
- })
+ // 编辑模式:如果有新文件,添加新文件;否则保留现有URLs
+ if (files.length > 0) {
+ files.forEach((file, index) => {
+ formData.append(`media_${index}`, file)
+ })
+ formData.append('media_count', files.length)
+ } else {
+ // 保留现有的media_urls
+ formData.append('existing_media_urls', JSON.stringify(mediaUrls))
+ }
+
+ await updatePost(postId, formData)
alert('更新成功!')
} else {
- await createPost({
- user_id: currentUserId,
- topic_id: topicId || undefined,
- title: title.trim(),
- content: content.trim(),
- media_urls: mediaUrls,
- status
+ // 创建模式:添加文件
+ files.forEach((file, index) => {
+ formData.append(`media_${index}`, file)
})
+ formData.append('media_count', files.length)
+
+ await createPost(formData)
alert('发布成功!')
}
navigate('/notebooks', { replace: true })