jeudi 21 septembre 2017

flask how to change the content of the web? [duplicate]

This question already has an answer here:

I have the code like this, which can upload the file. I want to process the upload file and show the download link after the processing. So how to change the content of the html when the process is done?

May be we can use the javascript to change it?

# -*- coding: utf-8 -*-
import os
from flask import Flask, request, url_for, send_from_directory
from werkzeug import secure_filename

ALLOWED_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif','txt'])

app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = os.getcwd()
app.config['MAX_CONTENT_LENGTH'] = 16 * 1024 * 1024


html = '''
    <!DOCTYPE html>
    <title>Upload File</title>
    <h1>图片上传</h1>
    <form method=post enctype=multipart/form-data>
         <input type=file name=file>
         <input type=submit value=上传>
    </form>
    <form method=post enctype=multipart/form-data>
         <input type=file name=file>
         <input type=submit value=上传>
    </form>
    '''

# html = html + '<a href=r"\\DESKTOP-0EVA06J\\Origami\\test.txt" download>下载</a>'
with open('template.html', encoding = 'utf-8') as the_file:
   html = the_file.read()


def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1] in ALLOWED_EXTENSIONS


@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'],
                               filename)


@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        file = request.files['file']
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            print('the file name will be ' + os.path.join(app.config['UPLOAD_FOLDER'], filename))
#the file will be here.

            file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))
            file_url = url_for('uploaded_file', filename=filename)
            # print('the file_url will be ' + file_url)
            return html + '<br><img src=' + file_url + '>'
    return html


if __name__ == '__main__':
    # app.run(host='192.168.0.176', port=5001)
    app.run(extra_files = html)
    # app.run()

Aucun commentaire:

Enregistrer un commentaire