使用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 ...
随机推荐
- java对数组的操作
1 拷贝数组 数组全拷贝 数组定位拷贝 2 判断数组是否相等(每个元素都对应相等) 3 数组和集合的相互转化 import java.util.Arrays; import java.util.Lis ...
- 【转】cocos2dx 3.x 集成protobuf
http://www.cnblogs.com/chevin/p/6001872.html vs2013+cocos2dx 3.13.1 这篇博文是集成Lua版本的protobuf,集成C++版本的过程 ...
- 好用的在线pdf转化器
https://smallpdf.com/cn/compress-pdf
- user32的使用
通过代码查询特定的窗口,并在文本框中输入文字然后单击"OK"按钮 需要查找的Dialog 使用Spy++查看窗口信息 通过代码实现功能 class Program { //defi ...
- calendar components
calendar components 日历 angular, react, vue ??? react https://github.com/intljusticemission/react-big ...
- 条件查询Criteria
public User getUserByNameCri(String name){ Session session = null; User user = null; try { session = ...
- npm+webpack+babel+react安装
npm+webpack+babel+react安装 1.首先要安装 Node.js, Node.js 自带了软件包管理器 npm 2.在项目文件目录下生成package.json # 进入项目目录$ ...
- 网络编程:listen函数
listen函数仅由TCP服务器调用,它做两件事: 当socket函数创建一个套接字时,它被假设为一个主动套接字,也就是说,它是一个将调用connect发起连接的客户套接字.listen函数把一个未连 ...
- HDU.2095(异或运算)
find your present (2) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- JavaScript使用数组拼接字符串性能如何?
传统上,字符串连接一直是js中性能最低的操作之一. view source print? 1 var text="Hello"; 2 text+=" World!&q ...