Rking14
Rking14

Reputation: 345

Download a file when button is pressed on web application?

I'm building a web app using python and Flask. I have implemented a button on the web app that calls a method in the python back-end and generates a .txt file which is saved in the current directory (where the current .html and .py files are stored). Once, the method finished running and the .txt file is generated, I want to be able to prompt the user to save the .txt file on the local computer (just like the windows prompt you get when you press "save as..." on a webpage).

Here is a simplified version of the web app

test.py

from flask import Flask, render_template, flash, request, redirect, url_for
import pandas as pd
# User Interface of the Web App
# App config.
DEBUG = True
app = Flask(__name__)
app.config.from_object(__name__)
app.config['SECRET_KEY'] = '123455677889900'



@app.route("/", methods=['GET', 'POST'])
def predict():
    if 'submit_test' in request.form:
    list = []
    for i in range(0, 100):
        list.append(i)

        list_df = pd.DataFrame(list)
        list_df.to_csv('test.txt', sep=',',index=False)
    return render_template('test.html')


if __name__ == "__main__":
    method_name = "main"

    app.run()  

test.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
    <head>
        <title>Model</title>
       <link rel="stylesheet" media="screen" href ="static/bootstrap/css/bootstrap.min.css">
       <link rel="stylesheet" href="static/bootstrap/css/bootstrap-theme.min.css">
       <meta name="viewport" content = "width=device-width, initial-scale=1.0">
       <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>

    </head>
    <body>


<div class="container">

  <h1>Model</h1>
  <br>
   <form  action="" method="post" role="form">
     <div class="row">
         <div class="col-12 col-md-6">
             <button name="submit_test", type="submit" class="btn btn-success" id="submit_test">Submit</button>
         </div>
     </div>

  </form>
  <a href="directory\test.txt" download="test"><button type=button">My Button</button></a>
</div>
</body>
<br>
</html>

Upvotes: 0

Views: 6680

Answers (2)

Arnav Chawla
Arnav Chawla

Reputation: 556

You can make the form redirect to a different flask url and return the download from flask.

@app.route('/uploads/<path:filename>', methods=['GET', 'POST'])
def download(filename):
    return send_from_directory(directory=os.getcwd()+"/files", filename="filename.txt")

Upvotes: 2

Justin T.
Justin T.

Reputation: 846

This should at least point you in the right direction:

<?php

//set path to file
$filePath = "path/to/file.txt";

//set file name (basename() returns the actual filename)
$fileName = basename($filePath);

//set content type
header('Content-Type: text/plain');

//set content disposition to 'attachment' to prompt download
header('Content-Disposition: attachment; filename="' . $fileName . '"');

//read the file and write its contents to the output buffer
readfile($filePath);

?>

Upvotes: 0

Related Questions