后端增加upload_file路由
This commit is contained in:
parent
a511f57a59
commit
55371ca42d
@ -129,7 +129,7 @@ export const uploadAttachment = async (
|
|||||||
formData.append('folder', 'Home')
|
formData.append('folder', 'Home')
|
||||||
|
|
||||||
const response = await axios.post(
|
const response = await axios.post(
|
||||||
`/api/action/upload_file`,
|
`/jingrow/upload_file`,
|
||||||
formData,
|
formData,
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
@ -334,7 +334,7 @@ export const uploadFileToJingrow = async (
|
|||||||
}
|
}
|
||||||
|
|
||||||
const response = await axios.post(
|
const response = await axios.post(
|
||||||
`/api/action/upload_file`,
|
`/jingrow/upload_file`,
|
||||||
formData,
|
formData,
|
||||||
{
|
{
|
||||||
headers: {
|
headers: {
|
||||||
|
|||||||
@ -5,7 +5,7 @@
|
|||||||
提供对云端数据库的增删改查操作
|
提供对云端数据库的增删改查操作
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from fastapi import APIRouter, HTTPException, Request
|
from fastapi import APIRouter, HTTPException, Request, UploadFile, File, Form
|
||||||
from fastapi.responses import JSONResponse
|
from fastapi.responses import JSONResponse
|
||||||
from typing import Dict, Any, Optional
|
from typing import Dict, Any, Optional
|
||||||
import json
|
import json
|
||||||
@ -198,5 +198,40 @@ async def get_record_count(request: Request, pagetype: str):
|
|||||||
raise HTTPException(status_code=500, detail=str(e))
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
|
||||||
|
|
||||||
|
@router.post("/jingrow/upload_file")
|
||||||
|
async def upload_file_api(
|
||||||
|
file: UploadFile = File(...),
|
||||||
|
attached_to_pagetype: Optional[str] = Form(None),
|
||||||
|
attached_to_name: Optional[str] = Form(None),
|
||||||
|
attached_to_field: Optional[str] = Form(None)
|
||||||
|
):
|
||||||
|
"""上传文件到Jingrow服务器"""
|
||||||
|
try:
|
||||||
|
# 读取文件内容
|
||||||
|
file_content = await file.read()
|
||||||
|
filename = file.filename or "uploaded_file"
|
||||||
|
|
||||||
|
# 调用 jingrow.upload_file
|
||||||
|
result = jingrow.upload_file(
|
||||||
|
file_data=file_content,
|
||||||
|
filename=filename,
|
||||||
|
attached_to_pagetype=attached_to_pagetype,
|
||||||
|
attached_to_name=attached_to_name,
|
||||||
|
attached_to_field=attached_to_field
|
||||||
|
)
|
||||||
|
|
||||||
|
# 检查结果
|
||||||
|
if result.get('success'):
|
||||||
|
return JSONResponse(content={
|
||||||
|
"message": {
|
||||||
|
"file_url": result.get('file_url'),
|
||||||
|
"file_name": result.get('file_name')
|
||||||
|
}
|
||||||
|
})
|
||||||
|
else:
|
||||||
|
raise HTTPException(status_code=400, detail=result.get('error', '上传文件失败'))
|
||||||
|
|
||||||
|
except HTTPException:
|
||||||
|
raise
|
||||||
|
except Exception as e:
|
||||||
|
raise HTTPException(status_code=500, detail=str(e))
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user