0

实际上我需要获取croppie javascript库生成的base64图像数据。图像数据如下所示。

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAgAElEQVR4Xs1dB5xU1dX/39em7c422GXpVZGmWACxImAviYnYY/1UjLF8iYkxxq4xlk+jscZek1iiiUajRiMWehEFAemwLGyfnfravd/v3Dd39zEsxpYyuO7szKv3f8r/nHvueQz/5a9NmzbFMu32KF/ju4JrIwX8XbkQ9WCoFEKU+55fLsBrOBcQnLX6QksLwdOciw7XE43cxwqmsRWciRUJs2bF5MkD8v/Nt8z+2y5uwYIt8Wg0ewDApmoMU4XAHgA0IQQAAfrlc3qvXgK+z8E5hy8EPMEgePCZ4LQHAxcCgtFvxoXgSwDxtg/xNi/zPzxm771z/01j8B8HRAhhLFmyZoLvO1M5F9MMQ5ukaZplmTo0Lbg8AoN+aNADQDT5N2MMmqbJzz3Pg0eAMID7vAgSAcfg+6ILGIlS8eX7vsO4PzuZMN+2DOPvEyaMnccY8/+TAP3HAPnkk8+HeZ44njEczzmfKEeu+KJBNgxA14IBJ2lnNNCcNIGkXpeA6HrwO/hcwBUeCp4jD6UxHa7nw3NpfOk4OmzHCY4JwLJ0xGMxdLa3orY6gdbWZlFRmZyTy2RfjOjm83vvv//G/wQw/3ZAPv109XDOxZVC4DTGmElSHpijbk0wDB2mGQDi+R503ZDmSpolj0MzTKkR9LfEkUChYzAyWb7UEAIgncmisqIanZ2dsG1XnqNffR0K+Rxy2RS456J/fR+0tjbBcWxYuiHNHQBX+PxpnbFf73PIISv/ncD82wD5/POG8a7rXsUY+w75BJJ8Jd0k/TSenPvyt2WZEMKBYWiIx2NdGkDgWZaFznSn1A4CynVd5HI5pDpTyOUyyKQ7kMsV0NHRAcfxkc3kYJgG6uv7Y9y4sXQSNGxaj2GD+iPb0Y5sLovy6t5IpTKoqqyAU7AJe4ADOrkd7rwshPjVxEMPXfDvAOZfDsiyZev6AM5vfC5m+L4Px3Hg2LYUbDIdJM00qLbtwL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M9OGRI2FrEjh844l+MPbz8OJZVHTK4nekQE4ZNRx6GsNQ9SLdGVze/JbdP2lpqpLCIojW6oRasBVlpgAiUaj8Fy+sibdPm70jBlUjbHDa6eAfPbJJ9dms5lrKBLnxcIBJphkSWqwv0iaw0xI2X3l0NVFKtNGx1HapgY3DI4yJbQfbae0UdFedTzNpOSkD803EdEttGQaYdZwvL/0Dcxa/xx4TQE5cNhpDeNqpuGk8ReitzcYHlq6TFZYYNQ1hK8vnC0u1ZiwGVMjrdI6ASAxWaTh+P7Ppx9zxC1fGpAlc5fs4rr5pbqpRbjMmPoBY/E4GA+qRIiYBFIRnmFTMyH0WVCaE3bWYbMUdphKc3YGcKlzDWuoMhlSgjUypwKaTzOLOrJ+BxpSG/C3j15DZCDDitQC2FYKum9gj+T+mLHHuahy+sBmlFHojlGUxocBCfsudR89aUXpZ0pYaHaTfK7vUe7NTdsuG3Xk947cYcaxRw35ePbsxznnZwTRdHeJjpztL5nkCT

这是我的 HTML

<form action="{% url 'profile' %}" enctype="multipart/form-data" method="post" class="pic-upload-form" id="form">{% csrf_token %}
      <div class="custom-file">
        {% bootstrap_form form %}
      </div>
      <!-- Croppie area -->
      <div id="upload-demo"></div>
      <input type="hidden" id="image-data" name="imagebase64">
      <button  type="button" class="btn btn-outline-info btn-custom upload-result">save</button>
 </form>

我将图像数据绑定到隐藏输入的 js 的最后一部分。

$('.upload-result').on('click', function (ev) {
    $uploadCrop.croppie('result', {
        type: 'base64',
        size: 'viewport'
    }).then(function (src) {
         console.log(src);

         // bind the image data to the hidden inpu id image-data
         $('#image-data').attr('src', src);
         $('#form').submit();
    });
});

这是我的看法

def user_profile_view(request):
  '''Handle the image of the user.'''

  if request.method == 'POST':
      form = UploadPicForm(request.POST, request.FILES)
      if form.is_valid():
        profile = form.save(commit=False)

        # get the base64 image data
        profile.image = request.FILES['imagebase64']
        profile.save()
        messages.success(request, 'your pic updated successfully.')
        return HttpResponseRedirect(reverse('learning_path_tracker:home'))
else:
    # form's user field populated by current user
    form = UploadPicForm(initial={'user': request.user})
return render(request, 'users/uploadpic.html', {'form': form})

我认为图像数据不是一个字符串,这就是 request.FILES 引发错误的原因。但是我该如何解决这个问题,在此先感谢。

4

1 回答 1

1

request.FILES仅包含使用 input 上传的文件type="file"。而您将图像数据作为 base64type="hidden"发送,使用它将数据作为字符串发送,而不是作为文件发送。

因此,图像应该出现在request.POST而不是request.FILES.

做这个:

profile.image = request.POST['imagebase64']

作为旁注,您应该使用字典的get()方法,而不是直接从字典中访问键,因为如果字典中不存在请求的键,它可以让您提供默认值。

profile.image = request.POST.get('imagebase64', "default value")
于 2019-01-01T16:40:53.993 回答