1

Yep, that's one more question of how do I upload the file from jquery client to django server, except that none of ~15 solutions found online worked for me. (and please do excuse my noobness -- I'm an infrequent user of web frameworks in general).

So, the setup looks like this:
Client side:

    var formData = new FormData();
    formData.append('file', response.output['file'], response.output['name'])
    $.ajax({
      url: "djangourl",
      data: formData,
      processData: false,
      contentType: false, // also tried setting 'multipart/form-data', no profit
      mimeType: 'multipart/form-data',
      type: 'POST',
      dataType: 'json',
      cache: false,
      success: function(data){
        alert(JSON.stringify(data));
      },
      error: function(data){
        alert(JSON.stringify(data));
      }
    });

Server side:

@require_http_methods(["POST"])
@csrf_exempt
def upload_document(request, project_id):
    try:
        if request.FILES:
            return HttpResponse(json.dumps("Yay!", default=json_util.default), status=200, content_type="application/json")
        else:
            return HttpResponse(json.dumps("Nah.", default=json_util.default), status=200, content_type="application/json")
    except Exception as e:
        return HttpResponseServerError(str(e))

And every time I try this, I get the empty request.FILES, any ideas why?

UPDATE: as it turns out, request.POST contains key 'file' which is bound to "[object FileList]" (literally this string), if that helps to clarify the situation anyhow.

2

1 Answer 1

1

As it turns out, response.output['file'] which I was receiving from frontend (Aurelia-based app) was an FileList object instead of single file (though no multiple specifier was given to <input> tag). Closing topic, thanks.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.