文件上传邮件发送
一、原生文件上传
form.html
<img src="{{ url_for('static',filename='img/17.jpg') }}" width="300" alt=""> <form action="" method="post" enctype="multipart/form-data"> <p>修改头像 <input type="file" name="file"></p> <p><input type="submit" value="提交"></p> </form>
manage.py
#文件上传的视图函数@app.route('/upload/',methods=['GET','POST'])def icon(): img_name = None if request.method == 'POST' and 'file' in request.files: # return '有文件上传了' file = request.files.get('file') filename = file.filename #拿到文件名称 #图片上传保存的路径 imgPath = os.path.join(os.getcwd(), 'static/upload/'+filename) file.save(imgPath) img_name = filename return render_template('user/change_icon.html',img_name=img_name)
使用wtf和bootstrap渲染文件上传
from flask import Flask,render_template,requestfrom flask_script import Managerfrom flask_wtf import FlaskFormfrom flask_wtf.file import FileField,FileAllowed,FileRequiredfrom wtforms import SubmitFieldfrom flask_uploads import UploadSet,patch_request_class,configure_uploads,IMAGESfrom flask_bootstrap import Bootstrapimport osfrom PIL import Imageapp = Flask(__name__)app.config['MAX_CONTENT_LENGTH'] = 1024*1024*64app.config['SECRET_KEY'] = 'abcdef'app.config['UPLOADED_PHOTOS_DEST'] = os.path.join(os.getcwd(),'static/upload')bootstrap = Bootstrap(app)file = UploadSet('photos',IMAGES)configure_uploads(app,file)patch_request_class(app,size=None)manager = Manager(app)#自定义一个文件上传的表单类class File(FlaskForm): photos = FileField('修改头像',validators=[FileRequired(message='文件不能为空'),FileAllowed(file,message='该文件类型不允许上传')]) submit = SubmitField('提交')@app.route('/')def index(): return render_template('index.html')#生成随机的图片名称def random_name(suffix,length=32): import string,random myStr = string.ascii_letters + '0123456789' return ''.join(random.choice(myStr) for i in range(length))+suffix@app.route('/upload/',methods=['GET','POST'])def icon(): form = File() img_url = None if form.validate_on_submit(): data = request.files.get('photos') suffix = os.path.splitext(data.filename)[-1] newName = random_name(suffix) file.save(data,name=newName) img_url = file.url(newName) img = Image.open(os.path.join(app.config['UPLOADED_PHOTOS_DEST'],newName)) print(img.size) # 获取图片大小 # 设置尺寸 img.thumbnail((128, 128)) # 当前缩放不是等比缩放 变成等比缩放 img.save(os.path.join(app.config['UPLOADED_PHOTOS_DEST'],'s_'+newName)) return render_template('user/wtf-uplods.html',form=form,img_url=img_url)if __name__ == '__main__': manager.run()
模板中的代码
from flask import Flask,render_template,requestfrom flask_script import Managerfrom flask_wtf import FlaskFormfrom flask_wtf.file import FileField,FileAllowed,FileRequiredfrom wtforms import SubmitFieldfrom flask_uploads import UploadSet,patch_request_class,configure_uploads,IMAGESfrom flask_bootstrap import Bootstrapimport osfrom PIL import Imageapp = Flask(__name__)app.config['MAX_CONTENT_LENGTH'] = 1024*1024*64app.config['SECRET_KEY'] = 'abcdef'app.config['UPLOADED_PHOTOS_DEST'] = os.path.join(os.getcwd(),'static/upload')bootstrap = Bootstrap(app)file = UploadSet('photos',IMAGES)configure_uploads(app,file)patch_request_class(app,size=None)manager = Manager(app)#自定义一个文件上传的表单类class File(FlaskForm): photos = FileField('修改头像',validators=[FileRequired(message='文件不能为空'),FileAllowed(file,message='该文件类型不允许上传')]) submit = SubmitField('提交')@app.route('/')def index(): return render_template('index.html')#生成随机的图片名称def random_name(suffix,length=32): import string,random myStr = string.ascii_letters + '0123456789' return ''.join(random.choice(myStr) for i in range(length))+suffix@app.route('/upload/',methods=['GET','POST'])def icon(): form = File() img_url = None if form.validate_on_submit(): data = request.files.get('photos') suffix = os.path.splitext(data.filename)[-1] newName = random_name(suffix) file.save(data,name=newName) img_url = file.url(newName) img = Image.open(os.path.join(app.config['UPLOADED_PHOTOS_DEST'],newName)) print(img.size) # 获取图片大小 # 设置尺寸 img.thumbnail((128, 128)) # 当前缩放不是等比缩放 变成等比缩放 img.save(os.path.join(app.config['UPLOADED_PHOTOS_DEST'],'s_'+newName)) return render_template('user/wtf-uplods.html',form=form,img_url=img_url)if __name__ == '__main__': manager.run()
新闻热点
疑难解答