简体   繁体   English

保存画布图像时Django request.FILES为空

[英]Django request.FILES is empty when saving canvas image

I am creating a webapp where the an image is shown to the user and the user can draw on the image (basically the image is in a canvas). 我正在创建一个webapp,其中向用户显示图像,并且用户可以在图像上绘画(基本上图像在画布上)。

Now once the user is done drawing, the user will press the save button and the image needs to be saved in static folder (not locally). 现在,一旦用户完成绘制,用户将按下保存按钮,并且图像需要保存在静态文件夹中(而不是本地)。 Since I am using Django to accomplish this; 由于我正在使用Django完成此操作; my request.FILES is always empty whenever I call the route. 每当我呼叫路线时,我的request.FILES始终为空。

My question is how can I save the canvas on which the user drew to a folder in the project and not locally. 我的问题是如何保存用户在其上绘制的画布到项目中的文件夹而不是本地。

index.html 的index.html

<div id="uploaderForm">
        <form enctype="multipart/form-data" action="{% url 'addImg' %}" method="post">
            <input type="hidden" for="image_data" id="image_data" name="image_data" />
        </form>

         <div class="uploaderItem" id="uploaderSubmit">
            <a href="#" onclick="addImage();">Add to the gallery</a>
         </div>
</div>

script.js 的script.js

function addImage()
{   
    console.log("In add image");
    var image_data = canvas.toDataURL();
    console.log("Image data:", image_data);
    $('#image_data').val(image_data);
    $('#uploaderForm > form').submit()

}

views.py views.py

def add(request):
    print request.FILES
    #print request.POST['image_data']
    return HttpResponse("You're looking at question")

the FILES array is reserved for files uploaded in file input fields through a multipart form. FILES数组保留用于通过多部分形式在文件输入字段中上载的文件。 What you're doing is passing a base-64 encoded string representing your image, to a hidden text field that is then sent to your server. 您正在做的是将表示您的图像的base-64编码的字符串传递到一个隐藏的文本字段,然后将其发送到您的服务器。 By your code, the image should be found here: 通过您的代码,可以在以下位置找到该图像:

image_data = request.POST.get('image_data')

It will be a base-64 string, so you'll need to decode it, the example below can be applied to almost any data URL, but the format will depend on your browser, so it's kinda tricky: 这是一个以64为基数的字符串,因此您需要对其进行解码,下面的示例几乎可以应用于任何数据URL,但是格式取决于您的浏览器,因此有点棘手:

import re
pattern = r'^data:(?P<mime_type>[^;]+);base64,(?P<image>.+)$'
result = re.match(pattern, image_data)
if result:
  mime_type = result.group('mime_type')
  image = result.group('image').decode('base64')

Be careful: by not using a file as transport, you are basically dumping the whole image in the server's memory, and that's expensive if you're planning to serve a lot of clients, and it's also time consuming, so your request could timeout before you are done with the image. 注意:通过不使用文件传输,基本上是将整个映像转储到服务器的内存中,如果您打算服务大量的客户端,这会很昂贵,而且这很耗时,因此您的请求可能会在超时之前您已完成图像处理。

To fix this, you should consider uploading the image through AJAX, which is a supported way to treat Blobs in javascript, that way you could use Django's file upload facilities more efficiently 要解决此问题,您应该考虑通过AJAX上传图像,这是在javascript中处理Blob的一种受支持的方式,这样您可以更有效地使用Django的文件上传功能

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM