0

I am trying to send image from an html page to Django view. But I am not able to attain the data. The following is my code.

<input type="file" id = "imageupload" onchange = "sendimg()" />

<script>
  var sendimg = function(){
    var img = document.getElementById("imageupload")
    $.ajax({
      type: "POST",
      url: "/test/",
      data: {
        "imgfile": img,
      },
      processData: false,
      contentType: false,
      success: function(data){
        console.log("success");
        console.log(data);
      },
      failure: function(data){
        console.log("failure");
        console.log(data);
      },
    });
  }
</script>

The Django view is

@csrf_exempt
def testimg(request):
  print "the request",request.POST
  data = {"data": "ok"}
  return JsonResponse(data)

The print statement gives

the request <QueryDict: {}>

What am I doing wrong ?

Shiladitya
  • 12,003
  • 15
  • 25
  • 38
Monish K Nair
  • 136
  • 1
  • 13

2 Answers2

0

please take a look on documenataion

and for Ajax see this answer

CristiC777
  • 481
  • 11
  • 20
0

Have you tried .val() to get contents of an input field? http://api.jquery.com/val/

ahorak
  • 123
  • 8