zoukankan      html  css  js  c++  java
  • flask下载文件---文件流

    html:
    <a name="downloadbtn" class="btn btn-success pull-right" href="/downloadfile/?filename=/root/allfile/123.txt">下载</a>

    py:

    @app.route('/downloadfile/', methods=['GET', 'POST'])

    def downloadfile():
    if request.method == 'GET':
    fullfilename = request.args.get('filename')
        # fullfilename = '/root/allfile/123.txt'

    fullfilenamelist = fullfilename.split('/')
    filename = fullfilenamelist[-1]
    filepath = fullfilename.replace('/%s'%filename, '')
    #普通下载
    # response = make_response(send_from_directory(filepath, filename, as_attachment=True))
    # response.headers["Content-Disposition"] = "attachment; filename={}".format(filepath.encode().decode('latin-1'))
    #return send_from_directory(filepath, filename, as_attachment=True)
    #流式读取
    def send_file():
    store_path = fullfilename
    with open(store_path, 'rb') as targetfile:
    while 1:
    data = targetfile.read(20 * 1024 * 1024) # 每次读取20M
    if not data:
    break
    yield data

    response = Response(send_file(), content_type='application/octet-stream')
    response.headers["Content-disposition"] = 'attachment; filename=%s' % filename # 如果不加上这行代码,导致下图的问题
    return response
    没有文件名,和文件格式,遇到这种情况,打开F12,查看response.headers 与正常的比较









  • 相关阅读:
    jedis scan实现keys功能
    java简单实现一个阻塞式线程池
    Swift运算符
    数组的使用(1)
    Linux 常用命令
    Task02:基础查询与排序
    Task01:初识数据库
    摩尔投票法
    面向对象暑期课程总结
    xpath+requests+peewee——CSDN论坛全方位爬虫
  • 原文地址:https://www.cnblogs.com/wozuilang-mdzz/p/9851981.html
Copyright © 2011-2022 走看看