首页
仓库
文档
nginx手册
Docker手册
workerman
Flask
PHP
python
RabbitMQ
其他
Linux
占位1
占位2
目录
app.py ```python app.config['UPLOAD_FOLDER'] = os.getcwd()+"\\upload\\" #定义上传目录 app.config['ALLOWED_EXTENSIONS'] = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'} #允许的扩展名 ``` ```python import os from werkzeug.utils import secure_filename from flask import Blueprint, current_app, render_template, request #其他略 #验证扩展名函数 def allowed_file(filename,ALLOWED_EXTENSIONS): return '.' in filename and \ filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS @index_index.route('/from') # /html上传界面 def from_html(): return render_template('index/from.html') @index_index.route('/upload',methods=['POST']) #上传文件 def upload(): if 'file' not in request.files: print('No file part') return '' file = request.files['file'] if file.filename == '': print('No selected file') return '' if file: app = current_app._get_current_object() if file and allowed_file(file.filename,app.config['ALLOWED_EXTENSIONS']): #验证扩展名 filename = secure_filename(file.filename) #返回安全的文件名 file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))#上传文件 return f'File uploaded successfully: {filename}' else: print("not file type") return '' ``` from.html ```html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>File Upload</h1> <form method="post" action="/upload" enctype="multipart/form-data"> <input type="file" name="file" /> <input type="submit" value="Upload" /> </form> </body> </html> ```