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:
JavaScript
x
14
14
1
@app.route("/test", methods=['GET', 'POST'])
2
def test():
3
if request.method == "POST":
4
print("hello1")
5
6
f = open("myfile.txt", "w")
7
f.write("content")
8
f.close()
9
10
f = open("myfile.txt", 'r')
11
12
return json.dumps({'success': True, 'data': f.read()}), 200, {'ContentType':
13
'application/json'}
14
Javascript:
JavaScript
1
26
26
1
$.ajax('/test', {
2
type: 'POST',
3
data: { myData: product},
4
dataType: 'json',
5
success: function(data, status, xhr) {
6
console.log('response')
7
8
console.log(data)
9
10
console.log(data['success'])
11
12
var bytes = new Uint8Array(data['data']); // pass your byte response to this constructor
13
14
var blob=new Blob([data['data']], {type: "text/plain"});// change resultByte to bytes
15
16
var link=document.createElement('a');
17
link.href=window.URL.createObjectURL(blob);
18
link.download="myfile.txt";
19
link.click();
20
21
},
22
error: function (jqXhr, testStatus, errorMessage) {
23
console.log("error")
24
}
25
});
26