gor*_*ora 14 python tornado image-upload
我看到使用pycurl的例子,但不能确定这是否可以使用?一些例子会有所帮助.谢谢.
Nik*_*nyh 18
这是实现龙卷风上传的演示应用程序.
这是服务器代码:
import tornado.httpserver, tornado.ioloop, tornado.options, tornado.web, os.path, random, string
from tornado.options import define, options
define("port", default=8888, help="run on the given port", type=int)
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", IndexHandler),
(r"/upload", UploadHandler)
]
tornado.web.Application.__init__(self, handlers)
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.render("upload_form.html")
class UploadHandler(tornado.web.RequestHandler):
def post(self):
file1 = self.request.files['file1'][0]
original_fname = file1['filename']
extension = os.path.splitext(original_fname)[1]
fname = ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(6))
final_filename= fname+extension
output_file = open("uploads/" + final_filename, 'w')
output_file.write(file1['body'])
self.finish("file" + final_filename + " is uploaded")
def main():
http_server = tornado.httpserver.HTTPServer(Application())
http_server.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
唯一的,你必须从这段代码中理解,文件内容位于self.request.files[<file_input_name>][0].
这是html代码:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<title>Tornado Upload Application</title>
</head>
<body>
<p><h1>Tornado Upload App</h1></p>
<form enctype="multipart/form-data" action="/upload" method="post">
File: <input type="file" name="file1" />
<br />
<br />
<input type="submit" value="upload" />
</form>
Run Code Online (Sandbox Code Playgroud)
使用文件时 - 请确保该表单具有enctype="multipart/form-data".
Qua*_*rew 17
这很简单:
<form action="/file" methods="POST"><!--your code--></form>
Run Code Online (Sandbox Code Playgroud)
在Python中:
class FileHandler(tornado.web.RequestHandler):
# get post data
file_body = self.request.files['filefieldname'][0]['body']
img = Image.open(StringIO.StringIO(file_body))
img.save("../img/", img.format)
Run Code Online (Sandbox Code Playgroud)
但不建议这样做,因为所有上传的数据都加载到RAM中; 最好的方法是使用nginx加载模块,但这很复杂.
小智 5
以前的代码返回错误的文件名和错误的编 以下代码有效:
import tornado.httpserver, tornado.ioloop, tornado.options, tornado.web, os.path, random, string
class Application(tornado.web.Application):
def __init__(self):
handlers = [
(r"/", IndexHandler),
(r"/upload", UploadHandler)
]
tornado.web.Application.__init__(self, handlers)
class IndexHandler(tornado.web.RequestHandler):
def get(self):
self.render("tornadoUpload.html")
class UploadHandler(tornado.web.RequestHandler):
def post(self):
file1 = self.request.files['file1'][0]
original_fname = file1['filename']
output_file = open("uploads/" + original_fname, 'wb')
output_file.write(file1['body'])
self.finish("file " + original_fname + " is uploaded")
settings = {
'template_path': 'templates',
'static_path': 'static',
"xsrf_cookies": False
}
application = tornado.web.Application([
(r"/", IndexHandler),
(r"/upload", UploadHandler)
], debug=True,**settings)
print "Server started."
if __name__ == "__main__":
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25107 次 |
| 最近记录: |