簡體   English   中英

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

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

我正在生成流程圖並使用 Flask 的 send_from_directory 為其提供服務。

這按預期工作:

在控制器中:

    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")

以及模型中的相關功能:

    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

所有這些都按預期工作; .png 文件在沒有頁面刷新的情況下提供,用戶可以下載它。

現在這里是另一塊代碼。 請注意這些是如何完全相同的:

控制器:

        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")

模型:

    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

我可以看到文件是在正確的位置使用正確的唯一標識符正確生成的。 我可以看到我正在傳遞正確的文件名和文件路徑。 一切都與其他代碼塊完全相同。 但是當文件被提供時,它是頁面的 HTML 文件,而不是指定的 png 文件。

我一整天都在嘗試調試它,但我什么也想不通,一切都與工作代碼相同,一切都正確完成,直到 send_from_directory 函數任意返回 HTML 模板。

請幫忙,我完全不知所措。

我想到了。 我的數據庫連接沒有被正確關閉。 我不知道這些是如何相關的,此時我沒有時間去了解。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM