本文實(shí)例為大家分享了python3-flask文件上傳操作的具體代碼,供大家參考,具體內(nèi)容如下
創(chuàng)新互聯(lián)是一家專業(yè)提供元寶山企業(yè)網(wǎng)站建設(shè),專注與成都網(wǎng)站制作、做網(wǎng)站、H5開發(fā)、小程序制作等業(yè)務(wù)。10年已為元寶山眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)站制作公司優(yōu)惠進(jìn)行中。# -*- coding: utf-8 -*- import os import uuid import platform from flask import Flask,request,redirect,url_for from werkzeug.utils import secure_filename if platform.system() == "Windows": slash = '\\' else: platform.system()=="Linux" slash = '/' UPLOAD_FOLDER = 'upload' ALLOW_EXTENSIONS = set(['html', 'htm', 'doc', 'docx', 'mht', 'pdf']) app = Flask(__name__) app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER #判斷文件夾是否存在,如果不存在則創(chuàng)建 if not os.path.exists(UPLOAD_FOLDER): os.makedirs(UPLOAD_FOLDER) else: pass # 判斷文件后綴是否在列表中 def allowed_file(filename): return '.' in filename and \ filename.rsplit('.', 1)[1] in ALLOW_EXTENSIONS @app.route('/',methods=['GET','POST']) def upload_file(): if request.method =='POST': #獲取post過來的文件名稱,從name=file參數(shù)中獲取 file = request.files['file'] if file and allowed_file(file.filename): # secure_filename方法會(huì)去掉文件名中的中文 filename = secure_filename(file.filename) #因?yàn)樯洗蔚奈募赡苡兄孛?,因此使用uuid保存文件 file_name = str(uuid.uuid4()) + '.' + filename.rsplit('.', 1)[1] file.save(os.path.join(app.config['UPLOAD_FOLDER'],file_name)) base_path = os.getcwd() file_path = base_path + slash + app.config['UPLOAD_FOLDER'] + slash + file_name print(file_path) return redirect(url_for('upload_file',filename = file_name)) return '''''' if __name__ == "__main__": app.run(host='0.0.0.0',port=5000)Upload new File Upload new File