1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > python bottle web框架上传静态文件与加载静态文件

python bottle web框架上传静态文件与加载静态文件

时间:2019-03-29 02:06:59

相关推荐

python bottle web框架上传静态文件与加载静态文件

文章目录

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

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。