Skip to content
Advertisement

Heroku App, Created File with Python, how can I get the download url?

with my Flask App I created some files to the heroku filesystem. I know that the files are deleted by each dyno restart.

In my html/javascript frontend I want to download it now via button.

The question is, how can I get the download url of these files in the heroku filesystem?

Thank you!

Advertisement

Answer

I got the solution for my problem:

Python:

@app.route("/test", methods=['GET', 'POST'])
def test():
if request.method == "POST":
    print("hello1")

    f = open("myfile.txt", "w")
    f.write("content")
    f.close()

    f = open("myfile.txt", 'r')

return json.dumps({'success': True, 'data': f.read()}), 200, {'ContentType': 
'application/json'}

Javascript:

$.ajax('/test', {
type: 'POST',
data: { myData: product},
dataType: 'json',
success: function(data, status, xhr) {
    console.log('response')

    console.log(data)

    console.log(data['success'])

    var bytes = new Uint8Array(data['data']); // pass your byte response to this constructor

    var blob=new Blob([data['data']], {type: "text/plain"});// change resultByte to bytes
      
    var link=document.createElement('a');
    link.href=window.URL.createObjectURL(blob);
    link.download="myfile.txt";
    link.click();

    },
    error: function (jqXhr, testStatus, errorMessage) {
        console.log("error")
    }
});
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement