Tornado file upload with the front end

Source: Internet
Author: User

In the form we get user submitted data, using Get_argument, check box using get_arguments, but the file is not the same, the use of request.files files.

Form File Upload

HTML code:

<! DOCTYPE html>

Attention:

Form file upload, be sure to set the Enctype parameters on the form form. Enctype= "Multipart/form-data". Otherwise the upload will not succeed.

Python code:

#!/usr/bin/env python#-*-coding:utf-8-*-import tornado.ioloopimport tornado.webclass MainHandler (        Tornado.web.RequestHandler): Def get (self): Self.render (' index.html ') def post (self, *args, **kwargs): File_metas = self.request.files["fff"] # print (file_metas) for meta in file_metas:file_name = M eta[' filename ' with open (file_name, ' WB ') as Up:up.write (meta[' body ') settings = {' Template _path ': ' template ',}application = Tornado.web.Application ([(R "/index", MainHandler),], **settings) if __name__ = = "__ma In__ ": Application.listen (8000) tornado.ioloop.IOLoop.instance (). Start () 

Description

1, the code self.request encapsulates all the content sent over the request.

2, Self.request.files: Can get all the information to upload the file. This method obtains a generator, which is implemented internally by yield, so when we take advantage of the object returned by this method, we cannot get the object inside by subscript, only through the iterative method.

3. FileName of the Iterated object: The file name that represents the upload .

4. The bodyof the Iterated object: represents the content of the uploaded file . The contents of the obtained file are in byte form.


Ajax Uploading Files
    • Native Ajax
    • Jquery


Native Ajax upload Files

HTML code:

<! DOCTYPE html>

Description

The code uses native Ajax for file uploads.


Key points:

1, get the file object, through Files[0], get the currently uploaded file object.

2. By Formdata (), instantiate an object Form object.

3. Then the parameter to be passed, the file append into the form object in comma-delimited form with the key and value.

4. The entire form object is then sent to the server.


Attention:

The background code is the same as the code above, unchanged. Note The file names you receive are synchronized.


jquery File Upload

HTML code:

<! DOCTYPE html>
 
   

Description

1, and the same as the original, is to appear to get the object of the current upload file. Files[0]; Then instantiate the Form object and append the content to be passed into the instantiated object form.

2, the background code, note the field name corresponding.


Key points:

Processdata:false and Contenttype:false. These 2 are the key.

The default jquery will partially process the data we upload. The above two pieces of code are to tell jquery not to process our files, otherwise we will not process our files completely.


iframe File Upload

When the native Ajax and jquery uploads, we are uploading the file by instantiating a form object . But instantiation of this form object is not all browsers exist, such as the low version of IE may not have qualified FormData object, the above method has compatibility, no form object can not be sent. Hence the use of a better compatibility to operate theIFRAME.

HTML code:

<! DOCTYPE html>
 
    

Key points:

1, document.getElementById (' My_form '). target = ' my_iframe ': This code is to get the IFRAME tag.

Target is the goal, as long as the form is set to target, when the form is submitted, it will be submitted to the target specified by that target. So the code above indicates that as soon as the form is submitted, it will be submitted to the IFRAME.

2, when the IFRAME operation will execute the Testt method, the Testt method is to obtain the information returned in the background, and print.

Tornado file upload with the front end

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.