文章目录
1 上传文件2 加载静态文件1 上传文件
# 上传文件@route('/upload', method=['POST','GET'])def upload():f = request.files.get('file')if f == '':return '文件不能为空'# 名字, 格式name, ext = os.path.splitext(f.raw_filename)if ext not in ('.png', '.jpg', '.jpeg'):return 'File extension not allowed.'# 保存文件f.save('static/file/' + name + ext)
注:
f.raw_filename
:支持文件名为中文
f.filename
:不支持
当文件名重复时,会报错文件重复
2 加载静态文件
@route('<filename:re:.*\.css|.*\.js|.*\.png|.*\.jpg|.*\.jpeg|.*\.gif|.*\.otf|.*\.eot|.*\.woff|.*\.mp3|.*\.map|.*\.mp4|.*\.json|.*\.mtn|.*\.svg|.*\.ttf|.*\.woff2.*\.swf>')def server_static(filename):"""定义static下所有的静态资源路径"""return static_file(filename, root='static/file/')
更多文章:有勇气的牛排—python
官方地址:/
官方QQ群交流: 779133600