flask 中关于返回 xlsx 的问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Ritter
V2EX    Python

flask 中关于返回 xlsx 的问题

  •  
  •   Ritter 2019-09-24 15:27:53 +08:00 3721 次点击
    这是一个创建于 2214 天前的主题,其中的信息可能已经有所发展或是发生改变。

    后端一个接口需要返回 xlsx 文件 生成 xlsx 用的是 openpyxl 但是每次返回拉取到 windows 下打开都是文件损坏 我对比了保存到本地和接口下载的两份 xlsx 文件 发现通过接口下载下来的 都比保存到本地的大 保存到本地的是 10216 个字节 但是下载的却有 16 700 k 我看了 http 请求的 content-length 确实是 10216 没错 但是为什么下载下来的会比本地保存的大啊? 试过 保存在 NameTemporaryFile BytesIO 都是一样 小白 求各路神仙搭救 TIM 图片 20190924152416.png

    16 条回复    2019-09-24 18:00:41 +08:00
    Ritter
        1
    Ritter  
    OP
       2019-09-24 15:31:45 +08:00
    我吐了 百度谷歌都找不到
    arrow8899
        2
    arrow8899  
       2019-09-24 15:39:29 +08:00
    发一下代码
    ETiV
        3
    ETiV  
       2019-09-24 15:45:27 +08:00


    昨儿煎蛋上看到的图,今儿又看到了 LZ 头像,要笑死我了。。。

    我不用 Python,但是遇到需要 Excel 打开的数据文件我都无脑返回 CSV 格式的

    另外,16700 KBytes 的文件打不开,还是 10216 Bytes 文件打不开。
    说明一下,可能对后面帮你解答的人有帮助。
    Ritter
        4
    Ritter  
    OP
       2019-09-24 15:46:33 +08:00
    @arrow8899
    ![TIM 图片 20190924154329.png]( https://i.loli.net/2019/09/24/wDrkT9diMzyGhOP.png)
    上面注释是保存在 BytesIO 的 下面是保存本地在返回的 都是一样
    ![TIM 图片 20190924154432.png]( https://i.loli.net/2019/09/24/JhV9WiRmY14TKxu.png)
    这是前端保存的
    libaibuaidufu
        5
    libaibuaidufu  
       2019-09-24 15:48:35 +08:00
    首先试试 最简单的下载 等否使用啊
    ```
    from flask import url_for
    downloadUrl = url_for("upload", filename="xx", _external=True)
    ```
    然后再排查其他问题。
    Ritter
        6
    Ritter  
    OP
       2019-09-24 15:49:29 +08:00
    @ETiV
    直接保存到本地的问题
    16 700 的是返回的 打开显示损坏
    T T
    Ritter
        7
    Ritter  
    OP
       2019-09-24 15:51:59 +08:00
    @Ritter 直接保存到本地的没问题 少个字
    Ritter
        8
    Ritter  
    OP
       2019-09-24 15:52:22 +08:00
    @libaibuaidufu 下载是没问题的
    libaibuaidufu
        9
    libaibuaidufu  
       2019-09-24 16:01:52 +08:00
    @Ritter 把 mimetype 去掉试试。。。。 反正他本身也可以获取类型
    Ritter
        10
    Ritter  
    OP
       2019-09-24 16:04:22 +08:00
    @libaibuaidufu 试过了 还是一样的
    arrow8899
        11
    arrow8899  
       2019-09-24 16:18:05 +08:00
    前端 saveAs type: "application/octet-stream"
    libaibuaidufu
        12
    libaibuaidufu  
       2019-09-24 16:19:32 +08:00
    @Ritter 我试了一下没有发现,和你一样的问题 。。。
    Ritter
        13
    Ritter  
    OP
       2019-09-24 17:25:04 +08:00
    @arrow8899 已经试过 还是不行 现在发现好像是前端插件的问题
    Ritter
        14
    Ritter  
    OP
       2019-09-24 17:25:11 +08:00
    @libaibuaidufu 好像是前端插件的问题
    Ritter
        15
    Ritter  
    OP
       2019-09-24 17:52:42 +08:00
    解决了 找了一天
    前端请求文件时 responseType 需要设置为"Blob"
    标识响应为二进制数据
    Vegetable
        16
    Vegetable  
       2019-09-24 18:00:41 +08:00
    后端不用看了,没问题,前端的问题.前端代码过于魔幻,你那个 data 确定是文件吗?为了验证你可以先将 xlsx 改成 1 个字符的 txt 文件来测试.这样前端比较容易判断数据是在哪里多出来的
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     5429 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 26ms UTC 08:11 PVG 16:11 LAX 01:11 JFK 04:11
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86