How to upload a file using Ajax on POST?
up vote
7
down vote
favorite
I know the topics aren't missing on this subject that's why I looked into a huge amount of posts about this topic and couldn't find something that'd satisfy me, so I'm trying to build it by myself.
All I want to do is to upload a file using Ajax with Django to avoid page refresh.
Here's what I did :
basic_upload.html :
<form method="post" id="creationOptionsForm" enctype="multipart/form-data">
% csrf_token %
<div>
form.name.errors
form.name
</div>
<div>
form.email.errors
form.email
</div>
<div>
form.logo.errors
form.logo
</div>
<input type="submit" value="submit">
</form>
Ajax.js :
$(document).on('submit', '#creationOptionsForm', function(e)
e.preventDefault();
var form_data = new FormData();
form_data.append('file', $('#creationOptionsForm').get(0).files);
$.ajax(
type:'POST',
url:'/designer/results/',
processData: false,
contentType: false,
data:
organisation:$('#id_organisation').val(),
email:$('#id_email').val(),
logo:form_data,
csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val(),
,
);
);
Views.py :
def creationResult(request):
if request.method == 'POST':
form = UserInfos(request.POST, request.FILES)
if form.is_valid():
photo = form.save()
...
...
(forms.py & urls.py are correctly configured, it is not necessary to include them).
I guess there's an issue with the way I do on the ajax.js since on debug (pdb) request.POST
return every datas except logo
.
What am I doing incorectly ?
jquery ajax django
add a comment |
up vote
7
down vote
favorite
I know the topics aren't missing on this subject that's why I looked into a huge amount of posts about this topic and couldn't find something that'd satisfy me, so I'm trying to build it by myself.
All I want to do is to upload a file using Ajax with Django to avoid page refresh.
Here's what I did :
basic_upload.html :
<form method="post" id="creationOptionsForm" enctype="multipart/form-data">
% csrf_token %
<div>
form.name.errors
form.name
</div>
<div>
form.email.errors
form.email
</div>
<div>
form.logo.errors
form.logo
</div>
<input type="submit" value="submit">
</form>
Ajax.js :
$(document).on('submit', '#creationOptionsForm', function(e)
e.preventDefault();
var form_data = new FormData();
form_data.append('file', $('#creationOptionsForm').get(0).files);
$.ajax(
type:'POST',
url:'/designer/results/',
processData: false,
contentType: false,
data:
organisation:$('#id_organisation').val(),
email:$('#id_email').val(),
logo:form_data,
csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val(),
,
);
);
Views.py :
def creationResult(request):
if request.method == 'POST':
form = UserInfos(request.POST, request.FILES)
if form.is_valid():
photo = form.save()
...
...
(forms.py & urls.py are correctly configured, it is not necessary to include them).
I guess there's an issue with the way I do on the ajax.js since on debug (pdb) request.POST
return every datas except logo
.
What am I doing incorectly ?
jquery ajax django
add a comment |
up vote
7
down vote
favorite
up vote
7
down vote
favorite
I know the topics aren't missing on this subject that's why I looked into a huge amount of posts about this topic and couldn't find something that'd satisfy me, so I'm trying to build it by myself.
All I want to do is to upload a file using Ajax with Django to avoid page refresh.
Here's what I did :
basic_upload.html :
<form method="post" id="creationOptionsForm" enctype="multipart/form-data">
% csrf_token %
<div>
form.name.errors
form.name
</div>
<div>
form.email.errors
form.email
</div>
<div>
form.logo.errors
form.logo
</div>
<input type="submit" value="submit">
</form>
Ajax.js :
$(document).on('submit', '#creationOptionsForm', function(e)
e.preventDefault();
var form_data = new FormData();
form_data.append('file', $('#creationOptionsForm').get(0).files);
$.ajax(
type:'POST',
url:'/designer/results/',
processData: false,
contentType: false,
data:
organisation:$('#id_organisation').val(),
email:$('#id_email').val(),
logo:form_data,
csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val(),
,
);
);
Views.py :
def creationResult(request):
if request.method == 'POST':
form = UserInfos(request.POST, request.FILES)
if form.is_valid():
photo = form.save()
...
...
(forms.py & urls.py are correctly configured, it is not necessary to include them).
I guess there's an issue with the way I do on the ajax.js since on debug (pdb) request.POST
return every datas except logo
.
What am I doing incorectly ?
jquery ajax django
I know the topics aren't missing on this subject that's why I looked into a huge amount of posts about this topic and couldn't find something that'd satisfy me, so I'm trying to build it by myself.
All I want to do is to upload a file using Ajax with Django to avoid page refresh.
Here's what I did :
basic_upload.html :
<form method="post" id="creationOptionsForm" enctype="multipart/form-data">
% csrf_token %
<div>
form.name.errors
form.name
</div>
<div>
form.email.errors
form.email
</div>
<div>
form.logo.errors
form.logo
</div>
<input type="submit" value="submit">
</form>
Ajax.js :
$(document).on('submit', '#creationOptionsForm', function(e)
e.preventDefault();
var form_data = new FormData();
form_data.append('file', $('#creationOptionsForm').get(0).files);
$.ajax(
type:'POST',
url:'/designer/results/',
processData: false,
contentType: false,
data:
organisation:$('#id_organisation').val(),
email:$('#id_email').val(),
logo:form_data,
csrfmiddlewaretoken:$('input[name=csrfmiddlewaretoken]').val(),
,
);
);
Views.py :
def creationResult(request):
if request.method == 'POST':
form = UserInfos(request.POST, request.FILES)
if form.is_valid():
photo = form.save()
...
...
(forms.py & urls.py are correctly configured, it is not necessary to include them).
I guess there's an issue with the way I do on the ajax.js since on debug (pdb) request.POST
return every datas except logo
.
What am I doing incorectly ?
jquery ajax django
jquery ajax django
edited Jun 22 '17 at 20:53
asked Jun 22 '17 at 19:39
Nuri Katsuki
1,90252552
1,90252552
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
11
down vote
accepted
The below method works for me, it will submit all form value as serialize()
. You will get all form input's inside request.POST
and logo request.FILES
Try this:
$(document).on('submit', '#creationOptionsForm', function(e)
e.preventDefault();
var form_data = new FormData($('#creationOptionsForm')[0]);
$.ajax(
type:'POST',
url:'/designer/results/',
processData: false,
contentType: false,
async: false,
cache: false,
data : form_data,
success: function(response)
);
);
Update:
basically async:false
will do ajax request and stop executing further js code till the time request get complete, because upload file might take some time to upload to server.
While cache
will force browser to not cache uploaded data to get updated data in ajax request
Official Documentation here
Wonderful, what doesasync
&cache
do ?
– Nuri Katsuki
Jun 22 '17 at 20:56
1
@Lindow updated answer, hope that helps :)
– Mahesh Singh Chouhan
Jun 22 '17 at 21:06
4
You do not needasync: false
anymore (on the contrary: if you set it, you'll get a warning in the Chrome console)
– Olivier Pons
Jan 12 at 15:03
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check xhr.spec.whatwg.org.
– Taioli Francesco
May 29 at 15:48
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
11
down vote
accepted
The below method works for me, it will submit all form value as serialize()
. You will get all form input's inside request.POST
and logo request.FILES
Try this:
$(document).on('submit', '#creationOptionsForm', function(e)
e.preventDefault();
var form_data = new FormData($('#creationOptionsForm')[0]);
$.ajax(
type:'POST',
url:'/designer/results/',
processData: false,
contentType: false,
async: false,
cache: false,
data : form_data,
success: function(response)
);
);
Update:
basically async:false
will do ajax request and stop executing further js code till the time request get complete, because upload file might take some time to upload to server.
While cache
will force browser to not cache uploaded data to get updated data in ajax request
Official Documentation here
Wonderful, what doesasync
&cache
do ?
– Nuri Katsuki
Jun 22 '17 at 20:56
1
@Lindow updated answer, hope that helps :)
– Mahesh Singh Chouhan
Jun 22 '17 at 21:06
4
You do not needasync: false
anymore (on the contrary: if you set it, you'll get a warning in the Chrome console)
– Olivier Pons
Jan 12 at 15:03
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check xhr.spec.whatwg.org.
– Taioli Francesco
May 29 at 15:48
add a comment |
up vote
11
down vote
accepted
The below method works for me, it will submit all form value as serialize()
. You will get all form input's inside request.POST
and logo request.FILES
Try this:
$(document).on('submit', '#creationOptionsForm', function(e)
e.preventDefault();
var form_data = new FormData($('#creationOptionsForm')[0]);
$.ajax(
type:'POST',
url:'/designer/results/',
processData: false,
contentType: false,
async: false,
cache: false,
data : form_data,
success: function(response)
);
);
Update:
basically async:false
will do ajax request and stop executing further js code till the time request get complete, because upload file might take some time to upload to server.
While cache
will force browser to not cache uploaded data to get updated data in ajax request
Official Documentation here
Wonderful, what doesasync
&cache
do ?
– Nuri Katsuki
Jun 22 '17 at 20:56
1
@Lindow updated answer, hope that helps :)
– Mahesh Singh Chouhan
Jun 22 '17 at 21:06
4
You do not needasync: false
anymore (on the contrary: if you set it, you'll get a warning in the Chrome console)
– Olivier Pons
Jan 12 at 15:03
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check xhr.spec.whatwg.org.
– Taioli Francesco
May 29 at 15:48
add a comment |
up vote
11
down vote
accepted
up vote
11
down vote
accepted
The below method works for me, it will submit all form value as serialize()
. You will get all form input's inside request.POST
and logo request.FILES
Try this:
$(document).on('submit', '#creationOptionsForm', function(e)
e.preventDefault();
var form_data = new FormData($('#creationOptionsForm')[0]);
$.ajax(
type:'POST',
url:'/designer/results/',
processData: false,
contentType: false,
async: false,
cache: false,
data : form_data,
success: function(response)
);
);
Update:
basically async:false
will do ajax request and stop executing further js code till the time request get complete, because upload file might take some time to upload to server.
While cache
will force browser to not cache uploaded data to get updated data in ajax request
Official Documentation here
The below method works for me, it will submit all form value as serialize()
. You will get all form input's inside request.POST
and logo request.FILES
Try this:
$(document).on('submit', '#creationOptionsForm', function(e)
e.preventDefault();
var form_data = new FormData($('#creationOptionsForm')[0]);
$.ajax(
type:'POST',
url:'/designer/results/',
processData: false,
contentType: false,
async: false,
cache: false,
data : form_data,
success: function(response)
);
);
Update:
basically async:false
will do ajax request and stop executing further js code till the time request get complete, because upload file might take some time to upload to server.
While cache
will force browser to not cache uploaded data to get updated data in ajax request
Official Documentation here
edited Jun 22 '17 at 21:05
answered Jun 22 '17 at 19:52
Mahesh Singh Chouhan
2,1901922
2,1901922
Wonderful, what doesasync
&cache
do ?
– Nuri Katsuki
Jun 22 '17 at 20:56
1
@Lindow updated answer, hope that helps :)
– Mahesh Singh Chouhan
Jun 22 '17 at 21:06
4
You do not needasync: false
anymore (on the contrary: if you set it, you'll get a warning in the Chrome console)
– Olivier Pons
Jan 12 at 15:03
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check xhr.spec.whatwg.org.
– Taioli Francesco
May 29 at 15:48
add a comment |
Wonderful, what doesasync
&cache
do ?
– Nuri Katsuki
Jun 22 '17 at 20:56
1
@Lindow updated answer, hope that helps :)
– Mahesh Singh Chouhan
Jun 22 '17 at 21:06
4
You do not needasync: false
anymore (on the contrary: if you set it, you'll get a warning in the Chrome console)
– Olivier Pons
Jan 12 at 15:03
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check xhr.spec.whatwg.org.
– Taioli Francesco
May 29 at 15:48
Wonderful, what does
async
& cache
do ?– Nuri Katsuki
Jun 22 '17 at 20:56
Wonderful, what does
async
& cache
do ?– Nuri Katsuki
Jun 22 '17 at 20:56
1
1
@Lindow updated answer, hope that helps :)
– Mahesh Singh Chouhan
Jun 22 '17 at 21:06
@Lindow updated answer, hope that helps :)
– Mahesh Singh Chouhan
Jun 22 '17 at 21:06
4
4
You do not need
async: false
anymore (on the contrary: if you set it, you'll get a warning in the Chrome console)– Olivier Pons
Jan 12 at 15:03
You do not need
async: false
anymore (on the contrary: if you set it, you'll get a warning in the Chrome console)– Olivier Pons
Jan 12 at 15:03
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check xhr.spec.whatwg.org.
– Taioli Francesco
May 29 at 15:48
Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check xhr.spec.whatwg.org.
– Taioli Francesco
May 29 at 15:48
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f44708023%2fhow-to-upload-a-file-using-ajax-on-post%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown