I need to upload the canvas image data to the server (database) on the fly, i.e., I need to create a form and with an input=file, and post the image data without any user interaction.
getElementById('thecanvaselement'); // ... maybe some more canvas drawing operations here var url = canvas. toDataURL(); The results (stored in the url variable) can then be sent back to the server using AJAX and then saved to your database like you would normally do.
The getImageData() method returns an ImageData object that copies the pixel data for the specified rectangle on a canvas. Note: The ImageData object is not a picture, it specifies a part (rectangle) on the canvas, and holds information of every pixel inside that rectangle.
FWIW, this is how I got it working.
My server is in google app engine. I send canvas.toDataURL()'s output as part of post request using jQuery.post. The data URL is base64 encoded image data. So on server I decode it and convert it to image
import re import base64 class TnUploadHandler(webapp.RequestHandler): dataUrlPattern = re.compile('data:image/(png|jpeg);base64,(.*)$') def post(self): uid = self.request.get('uid') img = self.request.get('img') imgb64 = self.dataUrlPattern.match(img).group(2) if imgb64 is not None and len(imgb64) > 0: thumbnail = Thumbnail( uid = uid, img = db.Blob(base64.b64decode(imgb64))) thumbnail.put()
From client I send the data like this:
$.post('/upload', { uid : uid, img : canvas.toDataURL('image/jpeg') }, function(data) {});
This may not be the best way to do it, but it works.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With