韩漫免费漫画在线观看方法,《好好疼爱里面》免费看,年轻漂亮的女士护士内衣,妈妈醉酒后把我当爸爸电视剧

文章 > Python基础教程 > python怎么实现文件上传界面

python怎么实现文件上传界面

头像

爱喝马黛茶的安东尼

2019-09-23 09:46:214412浏览 · 0收藏 · 0评论

本文章代码上传在码云上

代码地址

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表单实现)

<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="../static/css/toastr.min.css">
    <script src="../static/js/jquery.min.js"></script>
    <script src="../static/js/toastr.min.js"></script>
    <title>Document</title>
</head>
<body>
    <form method="POST" action="/upload" enctype="multipart/form-data">
        <input type="file" name="file" id="file">
        <input type="submit" value="upload">
        <a href=""></a>
    </form><hr>
    <ol id="filelist">
    </ol>
    <script>
        function checkstatus(){
            if('{{status}}'== 'OK'){
                toastr['success']("上传成功");
            }else if('{{status}}'== 'null'){
                toastr['error']("上传失败");           
            }
        }
        function get_list(){
            $.ajax({
                url:'/getlist',
                type:'GET',
                success:function(result){
                    len_result = result.length;
                    for(var x =0; x < len_result; x++){
                        $("#filelist").append('<br><a href=/download/' + result[x] + '>' + result[x] 
                        +'</a>');
                    }
                    alert(content_list);
                },
                error:function(){
                    alert("失败");
                }
            });
        }
    checkstatus();
    get_list();
    </script>
</body>
</html>

然后是后台接收

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/<filename>')
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')
关注

关注公众号,随时随地在线学习

本教程部分素材来源于网络,版权问题联系站长!

啊…啊…啊快日出水了A片真人 | 欧美寡妇性猛交XXX| 丈夫不在的日子中字头| 绝美儿媳妇苏酥| ALLEY| 欧美MV日韩MV最火的一句歌词 | 小蜜桃免费观看高清电视剧| 来真正的狗配人真人| 西游记| 三个老头拥着躁我一晚| 妈妈的新男友| 空姐4免费高清完整版观看| 按摩师把我添高潮了6次| 三亚私人高清影院的更新情况| 电影《满足2》完整版在线观看| 韩剧在办公室做饭的电影| 公交车上荫蒂添的好舒服H漫画| 来真正的狗配人真人| 朋友的母亲中字ID| 叔叔别撩我24集免费观看| 臣卜扌暴扌 圭辶鬲猛| 我把刘姨俩腿扛在肩上是哪一集的 | 在婚房被伴郎C了2个小时的后果| 一家乱战1-13集大| 丰年经继拇中文3的主要角色分析 妈妈出轨了免费观看电视剧全集 嫂嫂宝库免费观看电视剧全部 | C2000论坛| 玉女心经2:阴阳合欢李丽珍| 《工地夫妻》原声版| 约会大作战H| 白色变态蕾丝超薄开档连身袜| 亚洲| 我兄弟的妈妈免费观看电视剧| 《轮到你了》日剧| 韩国咬住奶头的乳三级| 《性房纵欲》在线观看| 嫩草精产一二三产区| 成都私人情侣免费看电视剧| 亚洲精品国产拍在线观看| 老师穿白色双开真丝旗袍怎么穿 | 适合做前看的MV| 人马杂配MV的视频在哪里看