我需要做一个简单的网页
功能和这个网页类似, 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()
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.