jquery - Cannot handle file upload in Django when request comes from Ajax and a form -


i'm trying handle file upload in django ajax post, doesn't work :(

below code in html file upload :

<form action="{% url 'saveprof' %}" method="post" enctype="multipart/form-data" id="form1">         {% csrf_token %}         <div class="profpic" style="background:url(../../../static/app/img/profile.png)">             <input type="file" id="picpath" name="picpath" class="uploadpic" value=""/>         </div>         </form>                     {% csrf_token %} 

save

below ajax method used post data :

function saveprof() { var formdata = {                 'picpath_aj': $('#picpath').val()             };         $.ajax({             type: "post",             url: "saveprof",             enctype: 'multipart/form-data',             async: true,             data: {                  formdata,                 'profemail_aj': $('#profemail').val(),                 'csrfmiddlewaretoken': $("input[name=csrfmiddlewaretoken]").val()             },             success: function (data, textstatus, jqxhr) {                 $('#message').html(data);             }         });     } 

is possible ?

how can use html form file upload , send data via above ajax method ?

here view

def saveprof(request):     if request.method == "post":         user_form1 = documentform(request.post, request.files)         if user_form1.is_valid():             user_form1.save()     #and code save other fields along user_form1 

form.py

class documentform(forms.modelform):     picpath = forms.filefield()     class meta:         model = td_student         fields = ('picpath',) 

model.py

class td_student(models.model):     picpath=models.filefield(upload_to=unique_filename)  def unique_filename(instance, filename):     ext = filename.split('.')[-1]     filename = "%s_%s.%s" %(uuid.uuid4(),time.strftime("%y%m%d_%h%m%s"), ext)     return os.path.join('documents/documents/'+time.strftime("%y%m%d"), filename 

when code being ran, user_form1.save() never gets executed failing validity condition in view.

i tried several hours figure out issue, couldn't get. please me in identifying wrong implementation in view ?

image not being saved in server :(.. , couldn't file name expected (example : 'documents/documents/20150716/a1f81a80-ce6f-446b-9b49-716b5c67a46e_20150716_222614.jpg' per unique_filename method )

please note can upload image when use html form instead of using ajax post.. requirement current implementation ajax only.

we have form this:

<form method="post" name="form" id="form" enctype="multipart/form-data">     <input type="file" id="img"/>     <input type="submit"/> </form> 

you have img chosen user javascript have this:

$(document).ready(function(){     $('form').on('submit', function(e){         e.preventdefault();         var data = new formdata($('form').get(0));         $.ajax({             url: :"/url",             method: "post",             data: data,             success: function(data){},             error: function(data){},             processdata: false,             contenttype: false,         });     }); }); 

and going able retrieve img chosen user in django with:

request.files 

Comments

Popular posts from this blog

php - Zend Framework / Skeleton-Application / Composer install issue -

c# - Better 64-bit byte array hash -

python - PyCharm Type error Message -