diff --git a/dsRag/Start.py b/dsRag/Start.py index 6e56680e..b283a32f 100644 --- a/dsRag/Start.py +++ b/dsRag/Start.py @@ -10,6 +10,8 @@ from fastapi.staticfiles import StaticFiles from openai import OpenAI from sse_starlette.sse import EventSourceResponse from gensim.models import KeyedVectors +from starlette.responses import StreamingResponse + from Config import Config from Config.Config import MS_MODEL_PATH, MS_MODEL_LIMIT, MS_HOST, MS_PORT, MS_MAX_CONNECTIONS, MS_NPROBE, DEEPSEEK_API_KEY, DEEPSEEK_URL, MS_COLLECTION_NAME from Milvus.Utils.MilvusCollectionManager import MilvusCollectionManager @@ -190,10 +192,16 @@ async def save_to_word(request: Request): doc.save(file_stream) file_stream.seek(0) + # 获取文件内容并计算长度 + file_content = file_stream.getvalue() + return StreamingResponse( - file_stream, + BytesIO(file_content), media_type="application/vnd.openxmlformats-officedocument.wordprocessingml.document", - headers={"Content-Disposition": "attachment; filename=小学数学问答.docx"} + headers={ + "Content-Disposition": "attachment; filename*=UTF-8''%E5%B0%8F%E5%AD%A6%E6%95%B0%E5%AD%A6%E9%97%AE%E7%AD%94.docx", + "Content-Length": str(len(file_content)) + } ) @app.post("/api/rag") diff --git a/dsRag/Test/output.docx b/dsRag/Test/output.docx index e389e623..8233eb8e 100644 Binary files a/dsRag/Test/output.docx and b/dsRag/Test/output.docx differ diff --git a/dsRag/static/ai.html b/dsRag/static/ai.html index b724b497..88d94ad8 100644 --- a/dsRag/static/ai.html +++ b/dsRag/static/ai.html @@ -191,15 +191,28 @@ }, body: JSON.stringify({html: htmlContent}) }) - .then(response => response.blob()) + .then(response => { + if (!response.ok) { + throw new Error('网络响应不正常'); + } + return response.blob(); + }) .then(blob => { + if (blob.size === 0) { + throw new Error('文件内容为空'); + } const url = window.URL.createObjectURL(blob); const a = document.createElement('a'); a.href = url; a.download = '小学数学问答.docx'; document.body.appendChild(a); a.click(); + window.URL.revokeObjectURL(url); document.body.removeChild(a); + }) + .catch(error => { + console.error('保存Word文档出错:', error); + alert('保存Word文档失败: ' + error.message); }); }