创新互联Python教程:python怎么实现文件上传界面
本文章代码上传在码云上

成都创新互联公司,为您提供重庆网站建设、成都网站制作、网站营销推广、网站开发设计,对服务成都火锅店设计等多个行业拥有丰富的网站建设及推广经验。成都创新互联公司网站建设公司成立于2013年,提供专业网站制作报价服务,我们深知市场的竞争激烈,认真对待每位客户,为客户提供赏心悦目的作品。 与客户共同发展进步,是我们永远的责任!
代码地址
git@gitee.com:DanYuJie/upanddown.git
这里我们使用flask框架,简单实用
目录结构: upandown/ static/ css/ js/ jquery.min.js toastr.min.js templates/ index.html test.py
相关推荐:《python基础教程》
首先我们需要一个页面在templates/index.html(这里使用form表单实现)
Document
然后是后台接收
test.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from flask import Flask,render_template, request, send_from_directory,jsonify, redirect
import os
# import sys
# reload(sys)
# sys.setdefaultencoding('utf-8')
app = Flask(__name__)
# ALLOWED_EXTENSTIONS = set(['png', 'jpg', 'jpeg', 'gif'])
app.config['UPLOAD_FOLDER'] = os.getcwd()
download_floder = app.config['UPLOAD_FOLDER'] + '/upload'
def allow_file(filename):
allow_list = ['png', 'PNG', 'jpg', 'doc', 'docx', 'txt', 'pdf', 'PDF', 'xls', 'rar', 'exe', 'md', 'zip']
a = filename.split('.')[1]
if a in allow_list:
return True
else:
return False
@app.route('/main')
def home():
return render_template('index.html')
@app.route('/getlist')
def getlist():
file_url_list = []
file_floder = app.config['UPLOAD_FOLDER'] + '/upload'
file_list = os.listdir(file_floder)
for filename in file_list:
file_url = url_for('download',filename=filename)
file_url_list.append(file_url)
# print file_list
return jsonify(file_list)
@app.route('/download/')
def download(filename):
return send_from_directory(download_floder,filename, as_attachment=True)
@app.route('/upload', methods=['POST', 'GET'])
def upload():
file = request.files['file']
if not file:
return render_template('index.html', status='null')
# print type(file)
if allow_file(file.filename):
file.save(os.path.join(app.config['UPLOAD_FOLDER']+'/upload/', file.filename))
return render_template('index.html', status='OK')
else:
return 'NO'
if __name__ == '__main__':
app.run(debug=True, host='0.0.0.0') 新闻标题:创新互联Python教程:python怎么实现文件上传界面
网站链接:http://jxjierui.cn/article/djcgggg.html


咨询
建站咨询
