user426795
user426795

Reputation: 11663

how to read a file using ajax and django?

My Ajax code:

var upfile = $("#file-id").val().split(/[\/\\]/).pop();
$.ajax({
    type: 'POST',
    url: '/upload/',
    data:{"upfile":upfile},
    success: function(data) {
    if (data['success'] === "true") {

    }
    },
    dataType: 'json'
   });
    return false;

Django code: In simple form submit action request.FILES.get('upfile') works. I can read the content of file using read() But in ajax, it is not working. even request.POST.get('upfile') gives me the filename string.

How to solve this issue?

Upvotes: 1

Views: 387

Answers (1)

Vincent
Vincent

Reputation: 630

It's normal, by default a form submitted with Ajax will not upload files. You need o have a look to some file upload jquery plugins (there's a few of them, I can not suggest one as I did not try any of these yet)

Upvotes: 2

Related Questions