jquery - Django ajax file upload -


so trying upload file without external plugins, running errors.

                <form method="" action="" name='upload_form' id='upload_form' >                     {% csrf_token %}                    <input type='file' name='file' id='file' />                    <input type='button' value='upload' id='upload'/>                 </form>                  <script type='text/javascript'>                 $(document).ready(function() {                     var csrf_token = $('input[name="csrfmiddlewaretoken"]').val();                     $('#upload').click(function() {                         $.ajax({                             csrfmiddlewaretoken: csrf_token,                             type: 'post',                             url : 'upload',                             enctype: "multipart/form-data",                             data  : {                                 'file': $('#file').val()                             },                             success: function(data) {                                 console.log(data)                             }                         })                     })                 })                 </script> 

my server :

class imageuploadview(loginrequiredmixin, jsonresponsemixin, ajaxresponsemixin, currentuseridmixin, view):      @method_decorator(csrf_protect)     def dispatch(self, *args, **kwargs):         return super(imageuploadview, self).dispatch(*args, **kwargs)      def post_ajax(self, request, username):                 print request.post.get('file', none)                 print request.files          # id = request.post['id']         # path = 'pictures/'         # f = request.files['picture']         # destination = open(path, 'wb+')         # chunk in f.chunks():         #   destination.write(chunk)         # destination.close() return httpresponse("image uploaded") 

i <multivaluedict: {}> request.files

how uploaded file code?

here use upload files using javascript, hope helps ! pass $('#file') parameter.

function upload(field, upload_url) {     if (field.files.length == 0) {         return;     }     file = field.files[0];     var formdata = new formdata();     formdata.append('file_upload', file);     $.ajax({         url: upload_url,         type: 'post',         data: formdata,         processdata: false,         contenttype: false,         success: console.log('success!')     }); } 

[edit]

and on server side (simplified):

def save_file(dest_path, f, filename):     original_name, file_extension = os.path.splitext(f.name)     filename = filename + '-' + datetime.datetime.now().strftime('%y-%m-%d-%h-%m-%s') + file_extension     url = '/' + dest_path + '/' + filename     path = django_settings.media_root + url     destination = open(path, 'wb+')     chunk in f.chunks():         destination.write(chunk)     destination.close()     return path  class fileuploadview(view):     def post(self, request, *args, **kwargs):         if request.files , request.files.get('file_upload'):             path = save_file(upload_to,                               request.files.get('file_upload'),                               filename)         return self.render_to_response({}) 

Comments

Popular posts from this blog

php - cannot display multiple markers in google maps v3 from traceroute result -

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -