1. Upload of files such as word Excel TXT Image in Python django framework,

Source: Internet
Author: User

1. Upload of files such as word Excel TXT Image in Python django framework,
1. File Upload (input tag)

(1) html code (form is submitted using the post method)

1 <input class = "btn-primary col-md-1" style = "margin: 0px 15px 25px 15px; "id =" submitForm "type =" button "value =" Submit "/> 2 <form id =" picture_form "action ="/addForm/"enctype =" multipart/form- data "method =" post "> 3 <table> 4 table 5 </table> 6 </form>

(2) Submit the jq form to the background

1 $ ("# submitForm "). click (function () {2 // alert ($ ("# SelectBus "). val (); 3 addNameForm (); // because the content of the dynamically loaded form is used, the name value of the tag given by the function is 4 $. ajaxSetup ({5 async: false 6}); 7 $ ("# picture_form "). ajaxSubmit ({8 resetForm: false, 9 dataType: 'json', 10 success: function (data) {11 if (data = 1) {alert ("submitted successfully ");} 12 else {alert ("failed to submit") ;}13} 14}); 15 });

(3) The python background processes the content transmitted by the form, mainly for file processing.

1 # custom storage Path 2 rollfileName = "webStatic/uploadfile/files/" 3 rollfilePath = OS. path. join (basePath, rollfileName) 4 # req. POST. get (text [1], '') if the information is obtained, the value is not 123. if it is null, the result of not obtaining the information is 123 5 if req. POST. get (text [1], '000000') = '000000': 6 # obtain the binary stream of a file. 7. reqfile = req. FILES [text [1] 8 # Get file name suffix 9 filetype = reqfile. name. split (". ") [-1] 10 # generate a random string with a suffix named 11 filename = str (uuid. uuid1 () + '. '+ filetype12 # open the file storage path 13 of = open (rollfilePath + filename, 'wb +') 14 # Write the file to the specified path 15 for chunk in reqfile. chunks (): 16. write (chunk) # write content 17. close () # close connection 18 # store the path rollfileName + filename in the database

(4) packages used for python background processing

1 # generate unordered string, replace file name 2 import uuid

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.