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