pythonflaskdata-uri

Flask Return File Given Data URI


Within my flask function I am able to retrieve the data URI (with selenium successfully) using the toDataURL() method on a canvas.

I would like to use the data from the data URI object to send a file in my flask method using the send_file flask method.

Thanks!


Solution

  • After working with it some more here's what solved my problem. I used the datauri package for python.

    Here's a sample route for Flask

    import datauri    
    import io
    from flask import Flask, send_file
    
    @app.route('/test', methods=["GET"])
    def return_file():
        data_uri = "some_data_uri"
        p = datauri.parse(data_uri)
        return send_file(io.BytesIO(p.data), mimetype=p.media_type)