一个tornado框架下的文件上传案例
- html部分----使用了form表单,注意三要素 method="post" action="/loaddata" enctype="multipart/form-data"
<form method="post" id="postform" action="/loaddata" enctype="multipart/form-data"> <div class="pricing-header px-3 py-3 pt-md-5 pb-md-4 mx-auto text-center"> <h3 class="display-4">选择要读取的文件</h3> <p class="lead"> <input type="file" name="damnfile" id="excelfile" class="form-control"> </p> </div> <div class="container"> <div class="card-deck mb-6 text-center"> <div class="card mb-6 box-shadow"> <div class="card-header"> <h4 class="my-0 font-weight-normal">Free</h4> </div> <div class="card-body" style="height:600px"> <h1 class="card-title pricing-card-title">$0 <small class="text-muted">/ mo</small></h1> <textarea cols="100%" rows="18"> </textarea> <button type="submit" id="postbtn" class="btn btn-lg btn-block btn-outline-primary">Sign up for free</button> </div> </div> </div> <footer class="pt-4 my-md-5 pt-md-5 border-top"> <div class="row"> <div class="col-12 col-md"> <img class="mb-2" src="https://getbootstrap.com/assets/brand/bootstrap-solid.svg" alt="" width="24" height="24"> <small class="d-block mb-3 text-muted">© 2017-2018</small> </div> <div class="col-6 col-md"> <h5>Features</h5> <ul class="list-unstyled text-small"> <li><a class="text-muted" href="#">Cool stuff</a></li> <li><a class="text-muted" href="#">Random feature</a></li> <li><a class="text-muted" href="#">Team feature</a></li> <li><a class="text-muted" href="#">Stuff for developers</a></li> <li><a class="text-muted" href="#">Another one</a></li> <li><a class="text-muted" href="#">Last time</a></li> </ul> </div> <div class="col-6 col-md"> <h5>Resources</h5> <ul class="list-unstyled text-small"> <li><a class="text-muted" href="#">Resource</a></li> <li><a class="text-muted" href="#">Resource name</a></li> <li><a class="text-muted" href="#">Another resource</a></li> <li><a class="text-muted" href="#">Final resource</a></li> </ul> </div> <div class="col-6 col-md"> <h5>About</h5> <ul class="list-unstyled text-small"> <li><a class="text-muted" href="#">Team</a></li> <li><a class="text-muted" href="#">Locations</a></li> <li><a class="text-muted" href="#">Privacy</a></li> <li><a class="text-muted" href="#">Terms</a></li> </ul> </div> </div> </footer> </div> </form>
- python部分
class loaddata(RequestHandler): def post(self): sourcefile =self.request.files['damnfile'] print(type(sourcefile)) for i in sourcefile: print(i) for meta in sourcefile: filename = meta['filename'] import os import time #realpath = os.path.(__file__) #print(realpath) with open("E:\\"+filename ,"wb") as writer: writer.write(meta['body'])