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.

whoever
  • 575
  • 4
  • 18

1 Answers1

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.

whoever
  • 575
  • 4
  • 18