My AJAX call sends a FormData
object with an uploaded image data inside it:
$(document).on('submit', '#profileImageForm', function(e){
e.preventDefault();
var form_data = new FormData();
var image = document.getElementById('id_banner_image').files[0].name;
form_data.append('file', image);
$.ajax({
type:'POST',
url: '/change_banner_image/',
data : {
form_data: form_data,
csrfmiddlewaretoken: $("input[name='csrfmiddlewaretoken']").val(),
},
traditional: true,
cache: false,
success: function(response){
console.log('Success');
},
});
});
and I succesfully receive the call in my views:
def change_banner_image(request):
if request.is_ajax():
data = request.POST.get('form_data')
profile = get_object_or_404(Profile, user=request.user)
profile.image = data
profile.save()
print(data)
return HttpResponse()
print(data)
prints: [object FormData]
. So how would I get the uploaded image from this FormData
object? Which will then be the value of profile.image
(which is a FileField
).