【问题标题】:How to create dynamic file sending algorithm with flask如何使用烧瓶创建动态文件发送算法
【发布时间】:2019-08-20 23:40:54
【问题描述】:

我在使用烧瓶提供文件时遇到一些问题。

首先,我的文件树:

processedMain
--processed1
--processed2
--...
--processedN

在每个文件夹中,我都有一堆文件,我将它们全部表示在表格中,我将 href 链接与每个文件的路径放在一起,并通过带有 send_file 返回的路由函数传递它。

问题是我能够使它工作的唯一方法是为每个硬编码的子文件夹创建@app.route,如下所示:

@app.route('/' + pathFits1[1] + '<id>', methods=['GET'])
def returnImg1(id):
    return send_file(pathFits1[1] + id, as_attachment=True, attachment_filename=id)

@app.route('/' + pathFits1[2] +'<id>', methods=['GET'])
def returnImg2(id):
    return send_file(pathFits1[2] + id, as_attachment=True, attachment_filename=id)

@app.route('/' + pathFits1[3] + '<id>', methods=['GET'])
def returnImg3(id):
    return send_file(pathFits1[3] + id, as_attachment=True, attachment_filename=id)

其中 pathFits1[i] 是子文件夹的路径,当用户单击 web 表中的文件时传递 id。

问题是我有很多子文件夹,并且为每个子文件夹创建 @app.route 很累。 这是否可以为每件事创建一条路线?

附: 我对烧瓶很陌生,所以如果我忘了说什么,请告诉我,我会填写。

解决方案

其实很简单。 正如@Dan.D 所指出的,flask 具有捕获变量的转换器,对我来说这就是我所需要的,因为我的 id 变量已经包含文件的完整路径。 所以现在只有这 3 行代码替代了一大堆或重复的代码:

@app.route('/<path:id>')
def returnImg(id):
    fileName = id.rsplit('/', 1)[-1]
    return send_file(id, as_attachment=True, attachment_filename=fileName)

其中 id 是文件的完整路径,path: 是路径转换器,fileName 是用户将在没有路径的情况下下载的文件名在里面(都在最后一个斜线之后)

【问题讨论】:

    标签: python flask sendfile


    【解决方案1】:

    您可以在路由表达式中捕获路径。来自文档:

    @app.route('/path/<path:subpath>')
    def show_subpath(subpath):
        # show the subpath after /path/
        return 'Subpath %s' % subpath
    

    来自http://flask.pocoo.org/docs/1.0/quickstart/

    【讨论】:

    • 好吧,我能说什么。就那么简单!非常感谢您的提示!我将向 OP 添加解决方案。
    【解决方案2】:

    据我所知,Flask 不支持@app.route URL 中的多个变量部分。但是,您可以将文件的完整路径设置为变量部分,然后对其进行解析以提取子文件夹路径和文件 ID。

    这是一个工作示例:

    pathFits1 = [None] * 3
    pathFits1[1] = 'content/images1/'
    pathFits1[2] = 'content/images2/'
    
    @app.route('/<path:fullpath>', methods=['GET'])
    def returnImg(fullpath):
        global pathFits1
        print("--> GET request: ", fullpath)
    
        # parse the full path to extract the subpath and ID
        # subpath: get everything until the last slash
        subpath = fullpath.rsplit('/', 1)[:-1][0] + '/'
        # id: get string after the last slash
        id = fullpath.rsplit('/', 1)[-1]
    
        print("ID:", id, "\tsubpath:", subpath)
    
        # try to send the file if the subpath is valid
        if subpath in pathFits1:
            print("valid path, attempting to sending file")
            try:
                return send_file(subpath + id, as_attachment=True, attachment_filename=id)
            except Exception as e:
                print(e)
                return "file not found"
        return "invalid path"
    

    但是,更好的实现方式是将文件 ID 和路径作为 GET 请求参数(例如 http://127.0.0.1:8000/returnimg?id=3&amp;path=content/images1/)发送并像这样检索它们:

    from flask import Flask, send_file, request
    
    app = Flask(__name__)
    
    pathFits1 = [None] * 3
    pathFits1[1] = 'content/images1/'
    pathFits1[2] = 'content/images2/'
    
    @app.route('/returnimg', methods=['GET'])
    def returnImg():
        global pathFits1
        id = request.args.get('id')
        path = request.args.get('path')
    
        print("ID:", id, "\tpath:", path)
    
        # try to send the file if the subpath is valid
        if path in pathFits1:
            print("valid path, attempting to sending file")
            try:
                return send_file(path + id, as_attachment=True, attachment_filename=id)
            except Exception as e:
                print(e)
                return "file not found"
        return "invalid path"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-03
      • 1970-01-01
      • 2021-08-19
      • 1970-01-01
      • 2020-04-20
      • 2019-05-13
      • 1970-01-01
      相关资源
      最近更新 更多