Pecan — 文件下载

以下载excel文件为例,仅摘取下载的主要代码:

import os
import pecan
from io import BytesIO
from openpyxl import Workbook
from mimetypes import guess_type
from urllib.parse import quote
from webob.static import FileIter

def download_file():
    file_name = 'tmp.xlsx'
    work_book = Workbook()
    bio = BytesIO()
    work_book.save(bio)
    bio.seek(0)
    pecan.response.app_iter = FileIter(bio)
    guess_content_type, _ = guess_type(file_name)
    pecan.request.pecan['content_type'] = guess_content_type
    pecan.response.headers['Content-Disposition'] = "attachment;filename*=UTF-8''" + quote(os.path.basename(file_name))

猜你喜欢

转载自blog.csdn.net/weixin_45804031/article/details/124852759
今日推荐