Ver código fonte

excel触发文件下载

xuyonghao 1 ano atrás
pai
commit
df14f81b4d
1 arquivos alterados com 6 adições e 2 exclusões
  1. 6 2
      app/api/excel.py

+ 6 - 2
app/api/excel.py

@@ -146,14 +146,18 @@ async def ws_excel(websocket: WebSocket,
 
 
 @router.get("/download/{file_full_name}")
-async def download_file(background_tasks: BackgroundTasks, file_full_name: str):
+async def download_file(file_full_name: str):
     file_name = os.path.basename(file_full_name)
     user_excel = EXCEL_FILES_PATH
     file_path = os.path.join(user_excel, file_full_name)
 
     if not os.path.exists(file_path):
         return JSONResponse(content={"error": "文件不存在"}, status_code=404)
-
+    return FileResponse(
+        path=file_path,
+        filename=file_name,
+        media_type='application/octet-stream',
+    )
     # def delete_file():
     #     try:
     #         os.unlink(file_path)