简体   繁体   中英

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.

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.

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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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