简体   繁体   English

Flask send_from_directory 提供 html 文件而不是指定文件

[英]Flask send_from_directory serving html file instead of specified file

I am generating flow charts and using Flask's send_from_directory to serve them.我正在生成流程图并使用 Flask 的 send_from_directory 为其提供服务。

This works as expected:这按预期工作:

in controller:在控制器中:

    elif fcf.validate_on_submit():
        subtopic = fcf.subtopicSelect.data
        topic = fcf.topicSelect.data
        t = Tree(topic, subtopic)
        token = t.getFlowChart()
        fileName = '{}.png'.format(token)
        filePath = SSTempDirectory
        return send_from_directory(filePath, fileName, as_attachment=True, attachment_filename = "flowchart.png")

and the relevant function in model:以及模型中的相关功能:

    def getFlowChart(self):
        graph = pydot.Dot(graph_type='digraph')


        nodeDict = {}
        for c in self.cards:
            newNode = pydot.Node(c.title, style="filled", fillcolor="#ffffff")
            nodeDict[c.id] = newNode
            graph.add_node(newNode)

        for c in self.cards:
            if self.triggers[c.id] != {}:
                for triggerID, trigger in self.triggers[c.id].items():
                    nodeA = nodeDict[trigger.cardId]
                    nodeB = nodeDict[trigger.destinationCardId]
                    graph.add_edge(pydot.Edge(nodeA, nodeB, label=trigger.label))

        token = secrets.token_urlsafe()
        graph.write_png(os.path.join(SSTempDirectory, '{}.png'.format(token)))

        return token

All of that works as expected;所有这些都按预期工作; the .png file is served without a page refresh and the user can download it. .png 文件在没有页面刷新的情况下提供,用户可以下载它。

Now here is another chunk of code.现在这里是另一块代码。 Note how these are the exact same:请注意这些是如何完全相同的:

controller:控制器:

        if af.hitCountSubmit.data:
            subtopic = af.subtopicSelect.data
            topic = af.topicSelect.data
            t = Tree(topic, subtopic)
            token = t.countCardHits()
            fileName = '{}.png'.format(token)
            filePath = SSTempDirectory
            return send_from_directory(filePath, fileName, as_attachment=True, attachment_filename = "flowchart.png")

model:模型:

    def getAnalyticGraph(self, cardHits):
        graph = pydot.Dot(graph_type='digraph')


        nodeDict = {}
        for c in self.cards:
            try:
                newNode = pydot.Node(c.title + " - " + str(cardHits[c.id]), style="filled", fillcolor="#ffffff")
            except KeyError:
                newNode = pydot.Node(c.title + " - " + str(0), style="filled", fillcolor="#ffffff")
            nodeDict[c.id] = newNode
            graph.add_node(newNode)

        for c in self.cards:
            if self.triggers[c.id] != {}:
                for triggerID, trigger in self.triggers[c.id].items():
                    nodeA = nodeDict[trigger.cardId]
                    nodeB = nodeDict[trigger.destinationCardId]
                    graph.add_edge(pydot.Edge(nodeA, nodeB))

        token = secrets.token_urlsafe()
        graph.write_png(os.path.join(SSTempDirectory, '{}.png'.format(token)))

        return token

I can see the file is generated correctly with the correct unique identifier in the correct location.我可以看到文件是在正确的位置使用正确的唯一标识符正确生成的。 I can see that I am passing the correct file name and file path.我可以看到我正在传递正确的文件名和文件路径。 Everything is the exact same as the other chunk of code.一切都与其他代码块完全相同。 But when the file is served, it is the HTML file for the page, and it is not the png file specified.但是当文件被提供时,它是页面的 HTML 文件,而不是指定的 png 文件。

I've been trying to debug this all day but I just can't figure anything out, everything is the same as the working code, everything is done correctly up until the send_from_directory function arbitrarily returns the HTML template.我一整天都在尝试调试它,但我什么也想不通,一切都与工作代码相同,一切都正确完成,直到 send_from_directory 函数任意返回 HTML 模板。

Please help, I'm at a total loss.请帮忙,我完全不知所措。

I figured it out.我想到了。 My database connection was not being properly closed.我的数据库连接没有被正确关闭。 I have no idea how these are related and at this point I don't have time to find out.我不知道这些是如何相关的,此时我没有时间去了解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM