flask大文件下载解决方案

flask大文件下载解决方案

文件数据小的话,可以直接使用flask自带的send_file()
读取文件或者直接写入缓冲区,再去读取都可以,StringIO, ByteIO,这样就是能写成不生层本地文件去下载的模式了

@app.route('download')
def download():
    file_hash = request.args.get('file')
    if file_hash:
        file = File.by_hash(file_hash)
        path = os.path.join('./output', '{}.csv'.format(file.filename.split('.')[0]))
        def file_send():
            store_path = path
            with open(store_path, 'rb') as targetfile:
                while 1:
                    data = targetfile.read(20 * 1024 * 1024)  # 每次读取20M
                    if not data:
                        break
                    yield data

        response = Response(file_send(), content_type='application/octet-stream')
        response.headers["Content-disposition"] = 'attachment; filename=%s.csv' % file.filename.split('.')[0]  # 如果不加上这行代码,导致下图的问题
        return response
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值