{% csrf_token %}
<label for="password">文件:<input type="file" name="ajax_file" id="id_file"></label>
<button id="id_button" >提交</button>
需要注意的是,forrmdata当中还可以添加普通字段的数据,appned括号当中格式还是一样的('传递到后端的名字',传递的数据)
<script>
$('#id_button').on('click',function () {
let formData = new FormData()
formData.append('fileData',$('#id_file')[0].files[0]),
$.ajax({
url:'',
type:'post',
processData:false,
contentType:false,
data:formData,
headers:{'X-CSRFToken': $('input[name=csrfmiddlewaretoken]').val()}, // csrf请求数据
success:function (data) {
alert(data)
}
})
})
</script>