【问题标题】:How to get image from input field using jQuery如何使用jQuery从输入字段中获取图像
【发布时间】:2018-04-11 13:02:05
【问题描述】:

我正在尝试将图像从 html 页面发送到 Django 视图。但我无法获得数据。以下是我的代码。

<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>

Django 视图是

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

打印语句给出

the request <QueryDict: {}>

我做错了什么?

【问题讨论】:

    标签: jquery django


    【解决方案1】:

    【讨论】:

      【解决方案2】:

      您是否尝试过 .val() 来获取输入字段的内容? http://api.jquery.com/val/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-02-14
        • 2023-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-09-15
        • 1970-01-01
        相关资源
        最近更新 更多