0

I have created a Canvas which can be drawn upon by the user. Now after drawing is done and as i hit the submit button i want it to reach the server in django. I can not figure out the javascript behind this and how to access it in django's views. What is the way of doing it.

  • 2
    Possible duplicate of [How to submit html5 canvas as part of form post?](https://stackoverflow.com/q/11708154/5223757) – wizzwizz4 Apr 13 '19 at 16:27
  • You should just be able to get JavaScript triggered on the `click` event of the button to send the data with a callback from `toBlob` and `XMLHttpRequest`, as described in the proposed duplicate. – wizzwizz4 Apr 13 '19 at 16:28

1 Answers1

0

I think there are a lot of ways to do this.

One which is: Assuming the images on canvas is all from the same origin, you can always call

const canvas=document.getElementById('canvas');
const imageUri=canvas.toDataUrl()

and then you can append it to a form input and submit to the backend just like you would for normal form

Martin Chuka
  • 109
  • 2
  • 5
  • Use [toBlob()](https://developer.mozilla.org/en-US/docs/Web/API/HTMLCanvasElement/toBlob) instead – Get Off My Lawn Apr 13 '19 at 16:48
  • this worked..!! thanks for help.
    {% csrf_token %}
    – BhavyaChoubisa Apr 14 '19 at 04:40
  • You are welcome @BhavyaChoubisa if my answer solved your question, click on the checkbox to accept it as an answer. – Martin Chuka Apr 14 '19 at 11:43