我看到使用pycurl的示例,但无法确定这是否可以使用?一些例子会有所帮助。谢谢。
答案 0 :(得分:16)
很简单:
<form action="/file" methods="POST"><!--your code--></form>
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)
但不推荐,因为所有上传的数据都加载到RAM中;最好的方法是使用nginx加载模块,但这很复杂。
答案 1 :(得分:16)
这是实现龙卷风上传的demo application。
这是服务器代码:
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()
您必须从此代码中了解到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>
使用文件时 - 请确保该表单有enctype="multipart/form-data"
。
答案 2 :(得分: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()
答案 3 :(得分:4)
我在使用['']语法访问文件属性时遇到了麻烦,不知道为什么,但我切换到点语法并且能够读取数据。我在Windows机器上,所以我还必须将'open(“static / public /”+ file_name,'w')'更改为'open(“static / public /”+ file_name,'wb')'。如果没有'wb',文件就会被破坏。
def uploadFile(self,input_name,file_type):
a_file = self.request.files[input_name][0]
extension = os.path.splitext(a_file.filename)[1]
if file_type is 'photo':
type_list = ['.png','.jpg','.jpeg','.gif']
elif file_type is 'attachment':
type_list = ['.pdf','.doc','.docx','.xls']
if extension in type_list:
file_name = ''.join(random.choice(string.ascii_lowercase + string.digits) for x in range(16))
output_file = open("static/public/" + file_name + extension, 'wb')
output_file.write(a_file.body)
return (a_file.filename + " has been uploaded.")
答案 4 :(得分:0)
lblError
有holder.operateInstanceImg.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String InstanceId = mInstanceModelList.get(position).getInstanceid().toString();
String instanceCurStatus = mInstanceModelList.get(position).getStatus();
final MenuDialog mdlgClickMenu = new MenuDialog(mActivity,R.style.menu_dialog,InstanceId,instanceCurStatus);
mdlgClickMenu.setContentView(R.layout.dialog_mdlgclickmenu);
mdlgClickMenu.show();
}
});
方法。
它的结果就像
tornado.web.RequestHandler