0

My reference is here How to send FormData objects with Ajax-requests in jQuery?

The answer on that link worked on my program. My problem is how to append more data in FormData? I'm using python django and I would like to know where I can put the csrfmiddlewaretoken and inputfilename

Before, this is what I have in form data

var form_data = {
    inputfilename: $("#filename").val(),
    inputfile: $("#file").val(),
    csrfmiddlewaretoken: $('input[name=csrfmiddlewaretoken]').val(),
    ajax: 1
};

and now,

var form_data = new FormData();    
form_data.append('file', input.files[0]);
Community
  • 1
  • 1
jned29
  • 477
  • 12
  • 50

1 Answers1

1

I'm not quite sure what's the issue you are having. Just append the csrf token the way you append the file:

var fd = new FormData();    
fd.append('file', input.files[0] );
fd.append('csrfmiddlewaretoken', csrf_token);
$.ajax({
  url: url,
  data: fd,
  processData: false,
  contentType: false,
  type: 'POST',
  success: callback
});
skoll
  • 2,272
  • 4
  • 30
  • 33
  • Are you sure you are sending the csrf token correctly? Is the request accepted, if you add `@csrf_exempt` to the Django view? – skoll Dec 20 '16 at 08:55
  • I changed this `input.files[0]` to `$('input[type="file"]')[0].files` and `csrf_token` to `$('input[name=csrfmiddlewaretoken]').val()` and so it works. – jned29 Dec 20 '16 at 09:46