<body> <input type="file" name="fafa" id="fafa"> <a href="">上傳</a> <input type="submit" value="提交" onclick="submitClick();"> <script> function submitClick() { var file_obj = document.getElementById('fafa').files[0]; var fd=new FormData(); fd.append('fafa',file_obj); var xhr =new XMLHttpRequest(); xhr.open('POST','/xinwen/uploadfile/',true); /*xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=utf-8');*/ xhr.send(fd);json
xhr.onreadystatechange=function () { if(xhr.readyState==4){ console.log(xhr.responseText); } } }
</script> </body>app
def uploadfile(request): file1=request.FILES.get('fafa')url
print(file1) with open(file1.name,'wb') as f: for item in file1.chunks(): f.write(item) f.close()code
ret = {'status': True, 'data': None} return HttpResponse(json.dumps(ret))orm