1. Uploading files such as word Excel TXT Image under Python Django framework

Source: Internet
Author: User

1. File upload (input tag)

(1) HTML code (form form submitted by post method only)

1 <inputclass= "btn btn-primary col-md-1"style= "margin:0px 15px 25px 15px;"ID= "SubmitForm"type= "button"value= "Submit" />2 <formID= "Picture_form"Action= "/addform/"enctype= "Multipart/form-data"Method= "POST">3   <Table>4 Table5   </Table>6 </form>

(2) JQ submits the form to the backstage

1$ ("#submitForm"). Click (function(){2     //Alert ($ ("#SelectBus"). Val ());3 addnameform ();//Because it is a dynamically loaded form content, it uses a function to give the tag character the name value4 $.ajaxsetup ({5Async:false6     });7$ ("#picture_form"). Ajaxsubmit ({8Resetform:false,9DataType: ' JSON ',TenSuccessfunction(data) { One             if(data=1) {Alert ("Submit succeeded");} A             Else{Alert ("Commit failed");} -         } -     }); the});

(3) The Python background accepts the contents of the processing form, the main file processing

1#Custom Storage Paths2Rollfilename="webstatic/uploadfile/files/"3Rollfilepath=Os.path.join (basepath,rollfilename)4 #req. Post.get (Text[1], ") If the information is obtained, then the value is not 123, if it is empty, no information is obtained and the result is 1235 ifReq. Post.get (Text[1],'123')=='123':6     #get file binary stream7Reqfile = req. Files[text[1]]8     #get file name suffix9Filetype=reqfile.name.split (".") [-1]Ten     #generate a random string plus suffix file name OneFilename=str (UUID.UUID1 ()) +'.'+filetype A     #Open File Store path -of = open (Rollfilepath+filename,'wb+') -     #writes a file to the specified path the      forChunkinchreqfile.chunks (): -Of.write (Chunk)#Write Content -Of.close ()#Close Connection - #store the path in the database Rollfilename+filename

(4) Python package used for background processing

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

1. Uploading files such as word Excel TXT Image under Python Django framework

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.