tornado之檔案上傳的幾種形式form,偽ajax(iframe)

來源:互聯網
上載者:User

標籤:web   os.path   and   load   攻擊   檔案上傳   利用   tip   xxxxxx   

1直接form提交給幕後處理

 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <title>form-upload</title> 6 </head> 7 <body>
  注意form的enctype類型"multipart/form-data"
 8 <form action="/form_upload" method="post" enctype="multipart/form-data">  9 <input type="file" name="file"/> 10 <input type="submit", value="上傳"/> 11 </form> 12 </body> 13 </html>

後台post方法統一處理代碼如下:
class FormRequest_handle(tornado.web.RequestHandler):    def get(self, *args, **kwargs):        self.render(‘iframe_upload.html‘, status=‘‘)    def post(self, *args, **kwargs):        print(‘post‘)        file_data = self.request.files[‘file‘]#讀出了的檔案是數組裡面的是字典形式[{‘filename‘:‘xxx‘,‘body‘:‘xxxxxxxxxxxxx‘}]        for meta in file_data:            filename = meta[‘filename‘]            print(filename, file_data)            with open(os.path.join(‘static‘, filename), ‘wb‘) as up:                up.write(meta[‘body‘])        self.write(‘upload success‘)settings = {    ‘template_path‘:‘views‘,    ‘static_path‘:‘static‘,    # ‘xsrf_cookies‘:True,此處測試csrf跨站偽造請求攻擊請忽略}if __name__ == ‘__main__‘:    application = tornado.web.Application([        # (r‘/index‘, Indexhandle),        # (r‘/manager‘, Managerdhandle),        # (r‘/csrf‘, Csrf_handle),        # (r‘/xml‘, XmlHttpRequest_handle),        (r‘/iframe‘,FormRequest_handle),    ], **settings)    application.listen(8089)    tornado.ioloop.IOLoop.instance().start()

  2.偽ajax(iframe) 此方法發送請求不重新整理頁面,利用iframe 的局部重新整理特性,對瀏覽器安全色性更好

 

 1 <body> 2 <form id="myform" action="/iframe" method="post" enctype="multipart/form-data"> 3     <input type="file" name="file"/> 4     <input type="button" value="上傳" onclick="redirect()"/> 5     <iframe id="myiframe" name="my_iframe"></iframe> 6 </form> 7 <script src="{{static_url(‘jquery-3.2.1.js‘)}}"></script> 8 <script> 9     function redirect() {10 //        document.getElementById(‘myiframe‘).onload = test;#執行完iframe立即執行test函數11         $(‘#myiframe‘).onload = test;12         document.getElementById(‘myform‘).target = ‘my_iframe‘;//此處等於IDmyframe會有不同13         document.getElementById(‘myform‘).submit();14 //        $(‘#myform‘).submit();15     }16    function test() {17         var t = $(‘#myiframe‘).contents().find(‘body‘).text();18         console.log(t)19    }20 </script>21 </body>

 

tornado之檔案上傳的幾種形式form,偽ajax(iframe)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.