新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:python怎么實(shí)現(xiàn)文件上傳界面
本文章代碼上傳在碼云上

成都創(chuàng)新互聯(lián)公司,為您提供重慶網(wǎng)站建設(shè)、成都網(wǎng)站制作、網(wǎng)站營銷推廣、網(wǎng)站開發(fā)設(shè)計(jì),對服務(wù)成都火鍋店設(shè)計(jì)等多個(gè)行業(yè)擁有豐富的網(wǎng)站建設(shè)及推廣經(jīng)驗(yàn)。成都創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)公司成立于2013年,提供專業(yè)網(wǎng)站制作報(bào)價(jià)服務(wù),我們深知市場的競爭激烈,認(rèn)真對待每位客戶,為客戶提供賞心悅目的作品。 與客戶共同發(fā)展進(jìn)步,是我們永遠(yuǎn)的責(zé)任!
代碼地址
[email protected]:DanYuJie/upanddown.git
這里我們使用flask框架,簡單實(shí)用
目錄結(jié)構(gòu): upandown/ static/ css/ js/ jquery.min.js toastr.min.js templates/ index.html test.py
相關(guān)推薦:《python基礎(chǔ)教程》
首先我們需要一個(gè)頁面在templates/index.html(這里使用form表單實(shí)現(xiàn))
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') 文章題目:創(chuàng)新互聯(lián)Python教程:python怎么實(shí)現(xiàn)文件上傳界面
分享URL:http://fisionsoft.com.cn/article/djcgggg.html


咨詢
建站咨詢
