From 7de92edb579d32b0f612681d9ddb15c21053fdd7 Mon Sep 17 00:00:00 2001 From: HuangHai <10402852@qq.com> Date: Thu, 26 Jun 2025 21:19:18 +0800 Subject: [PATCH] 'commit' --- dsRag/Start.py | 12 ++++++++++-- dsRag/Test/output.docx | Bin 36622 -> 36622 bytes dsRag/static/ai.html | 15 ++++++++++++++- 3 files changed, 24 insertions(+), 3 deletions(-) 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 e389e6231d1a3ce0d3d99fe3f67cdc6896d9b3c9..8233eb8e3358185ea47d4cb6b1cbef8c21221e6d 100644 GIT binary patch delta 268 zcmeC1$J95Ei6_9DnMH(wfq{cz&&rKFM;Mv*tlWHwQ65ZxW|{`3yII7UK;lc-;=wd4 zXBJqzkZYbkNLOy3YkfHTHp(9PIN20Ct*n0RR91 delta 268 zcmeC1$J95Ei6_9DnMH(wfq{d8Yxzc=BaBR3%Qs(Qln2wFnWlm1ZWeJSkoXd|creY% znFSUvTv^6t9rmH nU-meFc-p;oAS%1p5k#%)1)KT4*9*k6?sEZAjeTGx2m5>h#j0Hy 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); }); }