Tornado是一个和php有点像但又有不同的服务器脚本了,Tornado优点是它是非阻塞式服务器,而且速度相当快,下面来看个文件上传例子.
文件上传的内容体在tornado.web.RequestHandler.request.files属性中,并且是以数组形式存放的,使用临时文件存储时,在write完成后要记着把seek重置到文件头,要不然文件无法被读取.
再使用Image模块的thumbnail方法进行缩放时,resample=1作为重载渲染参数能够有效的使图片平滑,消除锯齿,代码如下:
if self.request.files: for f in self.request.files[ 'postfile' ]: rawname = f[ 'filename' ] dstname = str( int (time.time()))+ '.' +rawname.split( '.' ).pop() thbname = "thumb_" +dstname # write a file # src = "./static/upload/src/"+dstname # file(src,'w+').write(f['body']) tf = tempfile.NamedTemporaryFile() tf.write(f[ 'body' ]) tf.seek(0) # create normal file # img = Image.open(src) img = Image.open(tf.name) img.thumbnail((920,920),resample=1) img.save( "./static/upload/postfiles/" +dstname) # create thumb file//开源软件:phpfensi.com img.thumbnail((100,100),resample=1) img.save( "./static/upload/postfiles/" +thbname) tf.close()声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did29414