Front-End Code:
<style>. upload{Display:inline-block;padding:10px; Background-Color:brown; Position:absolute; top:0; bottom:0; right:0; left:0; Z-index:90; }. file{width:100px;height:50px;opacity:0; Position:absolute; top:0; bottom:0; right:0; left:0; Z-index:100; } </style><body>
<!--front-end upload file code-<div style="position:relative;width:100px;height:50px;"> <inputclass="file"Type="file"Id="Fafafa"Name="AFAFAF"/> <aclass="Upload"> Uploads </a> </div>
<!--front end submit files via XHR-<input type="Button"Value="Submit XHR"onclick="xhrsubmit ();"/>
<!--front end throughJQuerySubmit file--<input type="Button"Value="Submit jquery"onclick="jqsubmit ();"/> iframe Submit file --<form id="Form1"action="/upload_file/"Method="POST"Enctype="Multipart/form-data"target="IFM1"> <iframe id="IFM1"Name="IFM1"style="Display:none;"></iframe> <input type="file"Name="Fafafa"Onchange="Changeupalod ();"/>{#<input type= "Submit" onclick= "Iframesubmit ();" value= "form commits"/>#}</form> <div id="Preview"></div> <script src="/static/jquery-1.12.4.js"></script> <script>
Check Auto-Commitfunction Changeupalod () {$ ('#ifm1'). Load (function () {var text= $('#ifm1'). Contents (). Find ('Body'). text (); var obj=json.parse (text); $('#preview'). empty (); var Imgtag= Document.createelement ('img'); IMGTAG.SRC="/"+Obj.data; $('#preview'). Append (Imgtag); }); $('#form1'). Submit (); } //front- End Submit file function Jqsubmit () via JQuery {// $('#fafafa') [0] var file_obj= document.getElementById ('Fafafa'). Files[0]; var fd=new FormData (); Fd.append ('username','Root'); Fd.append ('Fafafa', File_obj); $.ajax ({URL:'/upload_file/', type:'POST', DATA:FD, Processdata:false,Tell JQuery notto process the data contenttype:false,Tell JQuery notTo set ContentType success:function (ARG,A1,A2) {console.log (ARG); Console.log (A1); Console.log (A2); }})}//Front end Submit file by XHR function Xhrsubmit () {// $('#fafafa') [0] var file_obj= document.getElementById ('Fafafa'). Files[0]; var fd=new FormData (); Fd.append ('username','Root'); Fd.append ('Fafafa', File_obj); var xhr=new XMLHttpRequest (); Xhr.open ('POST','/upload_file/', True); Xhr.onreadystatechange=function () {if(Xhr.readystate = = 4){ //receive complete var obj=Json.parse (Xhr.responsetext); Console.log (obj); } }; Xhr.send (FD); } function Iframesubmit () {$ ('#ifm1'). Load (function () {var text= $('#ifm1'). Contents (). Find ('Body'). text (); var obj=json.parse (text); $('#preview'). empty (); var Imgtag= Document.createelement ('img'); IMGTAG.SRC="/"+Obj.data; $('#preview'). Append (Imgtag); }) } </script></body>
The backend receives the file and returns the value <views> receive
defUpload_file (Request): Username= Request. Post.get ('username') Fafafa= Request. Files.get ('Fafafa') ImportOS Img_path= Os.path.join ('static/imgs/', Fafafa.name) with open (Img_path,'WB') as F: forIteminchfafafa.chunks (): F.write (item) RET= {'Code': True,'Data': Img_path}ImportJSONreturnHttpResponse (Json.dumps (ret))
Three ways to upload files in Django