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 ?

2 Answers 2

0

please take a look on documenataion

and for Ajax see this answer

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

Comments

0

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

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.