Answer the question
In order to leave comments, you need to log in
How to send files with AJAX in Django?
For example, to pass a regular text field, I do this:
var text = $('#form-new-task-z-r-text').val();
jQuery.ajax({
type: 'POST',
url: 'add_new_rec/'+prot_id+'/'+id_q+'/',
data: {csrfmiddlewaretoken: getCookie('csrftoken'), 'r_text':text},
success: function(data) {
}
});
var text = $('#form-new-task-z-r-text').val();
var my_file = $('#file_record').prop('files');
$.ajax({
type: 'POST',
url: 'add_new_rec/'+prot_id+'/'+id_q+'/',
async: true,
cache: false,
processData: false,
contentType: false,
enctype: 'multipart/form-data',
data: {csrfmiddlewaretoken: getCookie('csrftoken'), 'r_text':text, 'file':my_file},
success: function(data) {
//...
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question