HI all here i am submitting a form/ uploading a form i want to display that file has been successfully uploaded on the same page can anybody help me in how to complete the task after the uploading the file i go to the upload url but it is redirecting to the same html BUT i want to print a notification stating that the form has been submitted how can i achieve this
我在这里提交表格/上传表格我希望显示该文件已成功上传到同一页面任何人都可以帮助我如何完成任务后上传文件我去上传网址但它是重定向到相同的HTML但我想打印一个通知表明表单已经提交我如何实现这一点
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",message=None)
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'])
msg = "{} print in the same page".format(
self.get_body_argument("file1",""))
self.render("upload_form.html", message=msg)
#self.write("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()
and the HTML page is
和HTML页面是
enter code here
<html>
<body>
<p><h1>Tornado Upload App</h1></p>
<form enctype="multipart/form-data" action="/upload" method="post">
File: <input type="file" id="file1" />
<br />
<br />
<input type="submit" value="upload" />
</form>
<div id="form">
{% if message is not None %}
<p>{{ message }}</p>
{% end %}
</div>
</body>
</html>
1 个解决方案
#1
-1
Your problem is not about request handling in python, but about correct html form. You need to add name to your input:
你的问题不是关于python中的请求处理,而是关于正确的html表单。您需要为输入添加名称:
<input type="file" id="file1" />
to
至
<input type="file" name="file1" />
#1
-1
Your problem is not about request handling in python, but about correct html form. You need to add name to your input:
你的问题不是关于python中的请求处理,而是关于正确的html表单。您需要为输入添加名称:
<input type="file" id="file1" />
to
至
<input type="file" name="file1" />