Dzwebs.Net

撰写电脑技术杂文十余年

flask下载文件错误提示:The requested URL was not found on the server

Admin | 2022-2-4 22:43:20 | 被阅次数 | 8584

温馨提示!

如果未能解决您的问题,请点击搜索;登陆可复制文章,点击登陆

  本人最近在使用python的flask实现文件下载,但是却遇到如下的错误提示:

  The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again。

  经过实践与研究,最终找到了错误的根源及找到了解决问题的办法。

  原来的代码是这样的:

  try:
            return send_from_directory('./', "TempPaper.docx")
        except Exception as e:
            print('响应下载试卷异常:',e)
        return '下载异常'

  上述的代码,在调试模式下运行,居然没有任何问题,能够正常的下载。但是,导出为EXE脱离开发环境之后,就提示如题的错误信息了。

  后来改为如下代码即成功。

  try:
            return send_from_directory(os.getcwd(), "TempPaper.docx")
        except Exception as e:
            print('响应下载试卷异常:',e)
        return '下载异常'

  原因分析:

  os.getcwd()返回的是绝对路径,必须使用绝对路径,如D:\MyPythonProject\AddExam\AddExam,而不能使用相对路径 ./ ,所以,问题就这样被解决了。


该杂文来自: 最新技术

上一篇:c++生成DLL并调用的简单例子

下一篇:python-docx删除所有段落清空所有段落内容的方法

网站备案号:

网站备案号:滇ICP备11001339号-7

版权属性:

Copyright 2007-2021-forever Inc. all Rights Reserved.

联系方式:

Email:dzwebs@126.com QQ:83539231 访问统计