<body> <input type="file" name="fafa" id="fafa"> <a href="">上传</a> <input type="submit" value="提交" οnclick="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);
xhr.onreadystatechange=function () {
if(xhr.readyState==4){
console.log(xhr.responseText);
}
}
}
</script> </body>
def uploadfile(request): file1=request.FILES.get('fafa')
print(file1) with open(file1.name,'wb') as f: for item in file1.chunks(): f.write(item) f.close()
ret = {'status': True, 'data': None} return HttpResponse(json.dumps(ret))