使用bottle进行web开发(9):文件上传;json传递
1.文件上传
如果要完成文件上传,则需要对上文的form做一点改动,具体如下:
<form action="/upload" method="post" enctype="multipart/form-data">
Category: <input type="text" name="category" />
Select a file: <input type="file" name="upload" />
<input type="submit" value="Start upload" />
</form>
bottle把file的upload 是放在BaseRequest.files里的(以FileUpload进程的方式存在),这里,我们的例子,都是假设存在硬盘里的
@route('/upload', method='POST')
def do_upload():
category = request.forms.get('category')
upload = request.files.get('upload')
name, ext = os.path.splitext(upload.filename)
if ext not in ('.png','.jpg','.jpeg'):
return 'File extension not allowed.'
save_path = get_save_path_for_category(category)
upload.save(save_path) # appends upload.filename automatically
return 'OK'
2)有些js或者REST的客户端,发送application/json 给服务器,通过这个方式来传递信息。这个时候,BaseRequest.json属性就保存这些信息。
The raw request body
You can access the raw body data as a file-like object via BaseRequest.body. This is a BytesIO buffer or a
temporary file depending on the content length and BaseRequest.MEMFILE_MAX setting. In both cases the body
is completely buffered before you can access the attribute. If you expect huge amounts of data and want to get direct
unbuffered access to the stream, have a look at request[’wsgi.input’].
3)WSGI环境
每个的BaseRequest都保存着一个WSGI的环境字典。
举例如下;
@app.route('/my_ip')
def get_remote_ip():
ip=request.environ.get('REMOTE_ADDR')
return template('Your IP is:{{IP}}',IP=ip)
4)template
bottle自带一个模板,称之为:SimpleTemplate Engine
使用这个模板,可以通过template()函数或者view()装饰器。
只需要把模板名称和要替换的参数信息传递进去即可。
比如:
@route('/hello')
@route('/hello/<name>')
def hello(name='World'):
return template('hello_template', name=name)
bottle去哪里找这些模板呢:去./VIEWS/目录下或者Bottle.template_path环境变量。
Templates are cached in memory after compilation. Modifications made to the template files will have no affect until
you clear the template cache. Call bottle.TEMPLATES.clear() to do so. Caching is disabled in debug mode.
使用bottle进行web开发(9):文件上传;json传递的更多相关文章
- Web 开发中 文件上传 是出现的:java.io.FileNotFoundException: (文件名、目录名或卷标语法不正确。)
<span style="font-family: Arial, Helvetica, sans-serif; "> </span> <span st ...
- Java开发系列-文件上传
概述 Java开发中文件上传的方式有很多,常见的有servlet3.0.common-fileUpload.框架.不管哪种方式,对于文件上传的本质是不变的. 文件上传的准备 文件上传需要客户端跟服务都 ...
- web项目的文件上传和 下载
文件上传和下载在web应用中非常普遍,要在jsp环境中实现文件上传功能是非常容易的,因为网上有许多用Java开发的文件上传组件,本文以commons-fileupload组件为例,为jsp应用添加文件 ...
- JAVA Web 之 struts2文件上传下载演示(二)(转)
JAVA Web 之 struts2文件上传下载演示(二) 一.文件上传演示 详细查看本人的另一篇博客 http://titanseason.iteye.com/blog/1489397 二.文件下载 ...
- JAVA Web 之 struts2文件上传下载演示(一)(转)
JAVA Web 之 struts2文件上传下载演示(一) 一.文件上传演示 1.需要的jar包 大多数的jar包都是struts里面的,大家把jar包直接复制到WebContent/WEB-INF/ ...
- 基于 java 【Web安全】文件上传漏洞及目录遍历攻击
前言:web安全之文件上传漏洞,顺带讲一下目录遍历攻击.本文基于 java 写了一个示例. 原理 在上网的过程中,我们经常会将一些如图片.压缩包之类的文件上传到远端服务器进行保存.文件上传攻击指的是恶 ...
- SpringBoot | 第十七章:web应用开发之文件上传
前言 上一章节,我们讲解了利用模版引擎实现前端页面渲染,从而实现动态网页的功能,同时也提出了兼容jsp项目的解决方案.既然开始讲解web开发了,我们就接着继续往web这个方向继续吧.通常,我们在做we ...
- SpringBoot --web 应用开发之文件上传
原文出处: oKong 前言 上一章节,我们讲解了利用模版引擎实现前端页面渲染,从而实现动态网页的功能,同时也提出了兼容jsp项目的解决方案.既然开始讲解web开发了,我们就接着继续往web这个方向继 ...
- 04springMVC结构,mvc模式,spring-mvc流程,spring-mvc的第一个例子,三种handlerMapping,几种控制器,springmvc基于注解的开发,文件上传,拦截器,s
1. Spring-mvc介绍 1.1市面上流行的框架 Struts2(比较多) Springmvc(比较多而且属于上升的趋势) Struts1(即将被淘汰) 其他 1.2 spring-mv ...
随机推荐
- [整理]修改git 默认编辑器为vim
git config --global core.editor vim
- hash function比较
http://blog.csdn.net/kingstar158/article/details/8028635 由于工作需要,针对千万级别的数据,使用stl::map着实存在着效率问题,最后使用bo ...
- js+jquery 常用选择器函数
一.获取当前标签 JS: this,如下: <button onclick="fun(this)"></button> Jquery,如下: $(" ...
- Session接口常用方法
org.hibernate.Session接口 beginTransaction 开启事务 clear 清缓存 close 关闭session connection - 过时 获取Connection ...
- 2017博普杯 东北大学邀请赛(B. Drink too much water)(贪心+树链剖分)
题目地址:https://oj.neu.edu.cn/problem/1204 题目大意: 其实就是树上的线段覆盖, 给出一棵n个结点的树,然后给出树上的一些路径进行覆盖,然后要求选取最少的点,能够把 ...
- BZOJ2118 墨墨的等式 【最短路】
题目链接 BZOJ2118 题解 orz竟然是最短路 我们去\(0\)后取出最小的\(a[i]\),记为\(p\),然后考虑模\(p\)下的\(B\) 一个数\(i\)能被凑出,那么\(i + p\) ...
- Adore 解题报告
Adore 问题描述 小\(\text{w}\) 偶然间得到了\(1\)个 \(DAG\). 这个 \(DAG\) 有 \(m\) 层,第\(1\)层只有\(1\)个源点,最后\(1\)层只有\(1\ ...
- Codeforces 937.B Vile Grasshoppers
B. Vile Grasshoppers time limit per test 1 second memory limit per test 256 megabytes input standard ...
- 用JSR的@Inject代替@Autowired完成自动装配
从spring3.0开始spring支持JSR-330 的标准注解.主要是javax.inject这个包下的: 下面的例子用@Inject代替@Autowired.完成自动装配: MovieFinde ...
- 如何解决DuplicateFileException: Duplicate files copied in APK问题
问题:有重复的文件存在APK里 解决方案:在Module里的build.gradle中设置忽略此重复文件即可.