I am trying to upload a file in Django using Ajax. I have tried different approaches but I've get same results. The file cannot be read in the view.
Here is my codes:
#JS
$("#upload-file").click(function() {
var data = new FormData($('#fileForm').get(0));
var csrftoken = getCookie('csrftoken');
$.ajax({
type: "POST",
headers: {'X-CSRFToken': csrftoken},
url: "/countwords/",
data: data,
cache: false,
processData: false,
contentType: false,
success: function(json) {
var ss = json.len;
alert(ss);},
error: function() {
alert('error');}
});
});
finally here is my view codes:
if request.method == 'POST':
print request.FILES.keys()
print request.FILES
print request.POST.keys()
But I get following output when the code is run:
[]
MultiValueDict: {}
[]