flask文件下载

一、定义

  1. flask 文件下载
  2. python 接收tar 文件

二、实现

  1. flask 文件下载

    from flask import Flask, send_file, request
    import os
    from gevent.pywsgi import WSGIServer
    import logging

    logging.basicConfig(level=logging.INFO, format="%(asctime)s %(filename)s [line:%(lineno)d] %(levelname)s %(message)s")
    app = Flask(name)

    @app.route('/download', methods=["GET"])
    def download_tar():
    # 设置tar文件的路径
    tar_file_path = request.args.get("filename")
    tar_file_path = os.path.join("/home/jiayafei_linux/downloads_files", tar_file_path)
    logging.info(str(tar_file_path))
    # 检查文件是否存在
    if not os.path.isfile(tar_file_path):
    return "File not found", 404
    # 设置文件名,这将作为下载时的默认文件名
    filename = os.path.basename(tar_file_path)
    logging.info(str(filename))
    # 使用send_file发送文件
    return send_file(tar_file_path, as_attachment=True, download_name=filename)

    if name == 'main':
    print("start")
    http_server = WSGIServer(('0.0.0.0', int(8010)), app)
    http_server.serve_forever()

  2. python 接收tar 文件

    #接收tar 文件,并保存到本地 baike_10000k_0.tar
    def download_file_with_progress(self, url, save_path):
    response = requests.get(url, stream=True)
    total_size_in_bytes = int(response.headers.get('content-length', 0))
    block_size = 1024 # 1 Kibibyte
    progress_bar = tqdm(total=total_size_in_bytes, unit='iB', unit_scale=True)

    复制代码
         with open(save_path, 'wb') as file:
             for data in response.iter_content(block_size):
                 progress_bar.update(len(data))
                 file.write(data)
         progress_bar.close()
相关推荐
m0_734949793 小时前
MySQL如何配置定时清理过期备份文件_find命令与保留周期策略
jvm·数据库·python
m0_514520573 小时前
MySQL索引优化后性能没提升_通过EXPLAIN查看索引命中率
jvm·数据库·python
2601_949817723 小时前
Spring Boot3.3.X整合Mybatis-Plus
spring boot·后端·mybatis
H Journey3 小时前
Python 国内pip install 安装缓慢
python·pip·install 加速
uNke DEPH4 小时前
Spring Boot的项目结构
java·spring boot·后端
zhenxin01224 小时前
Spring Boot 3.x 系列【3】Spring Initializr快速创建Spring Boot项目
spring boot·后端·spring
Polar__Star5 小时前
如何在 AWS Lambda 中正确使用临时凭证生成 S3 预签名 URL
jvm·数据库·python
前端一小卒5 小时前
前端工程师的全栈焦虑,我用 60 天治好了
前端·javascript·后端
不停喝水5 小时前
【AI+Cursor】 告别切图仔,拥抱Vibe Coding: AI + Cursor 开启多模态全栈新纪元 (1)
前端·人工智能·后端·ai·ai编程·cursor
oyzz1205 小时前
Spring EL 表达式的简单介绍和使用
java·后端·spring