How to send and retrieve blob with Django

I have a blob . This is the image I changed using <canvas> . I verified that the data is correct by translating it into a url to validate it as an MDN guide . So far, so good. Now I would like to publish it on my Django server (along with some other inputs).

So, I am doing this:

 var fd = new FormData(form); canvas.toBlob( function(blob) { fd.set("image0", blob, "image0.jpg"); }, "image/jpeg", 0.7); var xhr = new XMLHttpRequest(); xhr.open('POST', '/ajax-upload/', true); xhr.setRequestHeader("X-CSRFToken", csrftoken); xhr.send(fd); 

I am checking the POST message using the network inspector console. My blob is confirmed as being sent with a POST request, and I can see how binary data is sent as the "image0" field.

 -----------------------------1773139883502878911993383390 Content-Disposition: form-data; name="image0"; filename="blob" Content-Type: image/png 

So, I am processing a POST request with this view, available at url /ajax-upload/ :

 def ajax_upload(request): if request.method == 'POST': print(request.POST.urlencode()) 

It doesn’t give me anything. As soon as I find out where my blob went, how can I turn it into Image ? Something like img = Image.open(request.POST["image0"]) ?

+5
source share
1 answer

BLOBs are binary data, so you can find it in request.body in Django. Its Bytes encoded (not Unicode).

HttpRequest.body An unhandled HTTP request object as a byte string. This is useful for processing data in different ways than regular HTML forms: binary images, XML payload, etc.

+2
source

Source: https://habr.com/ru/post/1244462/


All Articles