fastapi 框架文件上传,压缩包,下载文件

#上传作业
@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)

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
文本格式不能很好显示,请见谅(附件里有比较齐整的excel表格统计) 大小类型 传输类型 api方法 文件大小 花费时间 速率byte/ms 速率mb/s 缓存数组 次数 备注 大文件 下载 download_file(group_name, remote_filename, localFile) 1073741824(约1G) 28343ms 37883 36.12804413 无 1 下载 download_file(group_name, remote_filename , downloadStream) 1073741824(约1G) 29195ms 36778 35.07423401 0 1 fastDFS的DownloadStream,FileOutputStream 下载 download_file(group_name, remote_filename , downloadStream) 1073741824(约1G) 24352ms 44092 42.04940796 2K 1 fastDFS的DowloadStream,BufferedOutputStream 下载 download_file(group_name, remote_filename , DownloadCallback) 1073741824(约1G) 24831ms 43241 41.23783112 2K 1 实现DownloadCallback,BufferedOutputStream 下载 download_file(group_name, remote_filename , DownloadCallback) 1073741824(约1G) 25922ms 41422 39.50309753 8K 1 实现DownloadCallback,BufferedOutputStream 普通文件 下载 download_file(group_name, remote_filename, localFile) 59113472(约56M) 382ms 154747 147.5782394 无 1 下载 download_file(group_name, remote_filename , downloadStream) 59113472(约57M) 369ms 160199 152.7776718 0 1 fastDFS的DownloadStream,FileOutputStream 下载 download_file(group_name, remote_filename , downloadStream) 59113472(约58M) 499ms 118702 113.2030487 2K 1 fastDFS的DowloadStream,BufferedOutputStream 下载 download_file(group_name, remote_filename , DownloadCallback) 59113472(约59M) 592ms 99853 95.22724152 2K 1 实现DownloadCallback,BufferedOutputStream 下载建议:100M内数据使用fastDFS提供的DownloadStream;大于1G的数据,使用BufferedOutputStream和DowloadStream
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值