我需要做一个简单的网页
功能和这个网页类似, http://zhcn.109876543210.com/
选择文件---> 后端处理 ---> 处理完毕 ---> 提供下载链接。
对界面完全没有要求, 只想求一个具有类似功能的代码的框架, 自己学习了 flask
以下是我的代码, 现在不知道如何在处理完毕之后提供下载的链接。
多谢各位
# -*- 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()
![]() | 1 pipapa 2017-09-22 17:12:33 +08:00 前端利用 ajax,POST 数据,获取返回值 |
2 gosky 2017-09-22 17:42:39 +08:00 对 flask 了解有限 post /upload 处理,保存文件到{uuid}.data get /results/{uuid}.data |