30 lines
877 B
Python
30 lines
877 B
Python
|
|
|
|
import os
|
|
from fastapi import File, UploadFile
|
|
import requests
|
|
|
|
from configs.kb_config import KB_CHAT_TEMP_DIR
|
|
|
|
async def upload_file(file: UploadFile = File(...)):
|
|
# 指定临时文件夹路径
|
|
specified_dir = KB_CHAT_TEMP_DIR
|
|
os.makedirs(specified_dir, exist_ok=True) # 确保目录存在
|
|
# 获取文件名
|
|
filename = file.filename
|
|
|
|
# 读取文件内容
|
|
file_content = await file.read()
|
|
|
|
# 保存文件到磁盘
|
|
save_file_path = os.path.join(specified_dir, filename)
|
|
with open(save_file_path, 'wb') as f:
|
|
f.write(file_content)
|
|
url = "http://localhost:10000/convert"
|
|
|
|
data = {"path": f"{save_file_path}"}
|
|
|
|
query= requests.post(url, json=data)
|
|
with open(f"{save_file_path}.txt", 'w',encoding="utf-8") as f:
|
|
f.write(query.text)
|
|
return {"message": "文件上传成功", "filename": file.filename} |