Python + django implement File Upload

Source: Internet
Author: User
This series focuses on operability and introduces how to implement some simple functions through the djangoweb framework. Each article has integrity and independence. Beginners can understand the web development process in the hands-on process. For details, see relevant documents. 1. File Upload (input tag)

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


(2) Submit the jq form to the background

$ ("# SubmitForm "). click (function () {// alert ($ ("# SelectBus "). val (); addNameForm (); // because the content of the dynamically loaded form is used, the name value of the tag given by the function is $. ajaxSetup ({async: false}); $ ("# picture_form "). ajaxSubmit ({resetForm: false, dataType: 'json', success: function (data) {if (data = 1) {alert ("submitted successfully ");} else {alert ("failed to submit ");}}});});

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

# Custom storage path rollfileName = "webStatic/uploadfile/files/" rollfilePath = OS. path. join (basePath, rollfileName) # req. POST. get (text [1], '') if the information is obtained, the value is not 123. if the information is null, the result is 123 if req. POST. get (text [1], '000000') = '000000': # obtain the binary stream reqfile = req. FILES [text [1] # obtain the filename suffix filetype = reqfile. name. split (". ") [-1] # generate a random string with a suffix filename = str (uuid. uuid1 () + '. '+ filetype # open the file storage path of = open (rollfilePath + filename, 'wb +') # Write the file for chunk in reqfile to the specified path. chunks ():. write (chunk) # write content. close () # close the connection

18 # Storage path rollfileName + filename in the database

(4) packages used for python background processing

1 # generate an unordered string and replace the 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.