M
M
Mimuss2018-02-11 03:28:29
Django
Mimuss, 2018-02-11 03:28:29

How to send binary data to shingle with ajax?

Let's say there is this:

var data = new FormData(this);
                    data.append('csrfmiddlewaretoken', Cookies.get('csrftoken'));
                    data.append('author', meta.creator);
                    data.append('title', meta.bookTitle);
                    data.append('enctype', 'multipart/form-data');
                    $.ajax({
                        url: 'ajax/add/',
                        type: 'POST',
                        cache: false,
                        data: data,
                        processData: false,
                        contentType: false,
                        success: function (data) {
                            console.log('success')
                        },
                        error: function (data) {
                            console.log('error at books/ajax/add')
                        }
                    })

and here is such a twist
def add_book(request):
    if request.method == 'POST':
        print(request.FILES.get('epub'))
        print(request.POST.get('title'))
        print(request.POST.get('author'))
    else:
        return HttpResponseBadRequest()


for some reason it outputs this:
None
War and Peace
Lev Nikolayevich Tolstoy

That is, there is no 'epub' in the request.FILES dictionary at all
, the form itself
<form method="POST" enctype="multipart/form-data" action="{% url 'books:add' %}">
        <input type="file" accept="application/epub+zip" id="bookChooser" name="epub">
        <button class="btn btn-primary">Submit</button>
    </form>

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question