#上传作业
@home_work.post('home/work/docx/code')
def home_work_docx_code(file:UploadFile):
print("file上传的文件:",file)
path = os.path.join('interface/homeWork/source/homework',file.filename)
with open(path,'wb') as f:
for file_data in file.file:
f.write(file_data)
return {
"code":200,
"message":"上传成功"
}
@home_work.post('home/work/docx/zip')
def home_work_docx_code(file:UploadFile):
print("file上传的压缩包:",file.filename)
path = os.path.join("interface/homeWork/source/zip", file.filename)
print("path:",path)
with open(path, "wb") as f:
for file_data in file.file:
f.write(file_data)
return {
"code":200,
"message":"上传成功"
}
@home_work.get('/home/word/content')
def home_word_content():
return {
"code": 200,
"message": "上传成功",
}
@home_work.get("/student/homework/download/file")
async def download_docx():
# 指定文件的路径
file_path = Path(__file__).parent / "source/homework/static" / "example.docx"
# 检查文件是否存在
if not file_path.exists():
raise HTTPException(status_code=status.NOT_FOUND, detail="File not found")
# 设置响应头
file_name = file_path.name
content_type = "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
# 返回 FileResponse
return FileResponse(file_path, media_type=content_type, filename=file_name)
fastapi 框架文件上传,压缩包,下载文件
最新推荐文章于 2024-07-21 00:15:09 发布