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//開源軟件:Vevb.com
- img.thumbnail((100,100),resample=1)
- img.save("./static/upload/postfiles/"+thbname)
- tf.close()
新聞熱點
疑難解答