I am trying to save an html canvas content to Google Storage. What I do is as following :
- Create a signed url for the image file.
class ThumbnailUploadSignedURL(APIView): @method_decorator(login_required(login_url='/login/')) def post(self, request): filename = request.POST.get('filename') filetype = request.POST.get('type') filesize = request.POST.get('size', 0) path = '{0}-{1}/{2}/'.format(request.user, request.user.id, thumbnail.name) blob = default_storage.open(full_path, 'wb') signed_url = blob.blob.generate_signed_url(expiration=default_storage.expiration, method='PUT', content_type='image/png') return Response({"thumbnail_signed_url":signed_url})
- Get the content of canvas and send it to Google Storage.
var image = canvas.toDataURL("image/png"); const xhr = new XMLHttpRequest(); xhr.open("PUT", data.thumbnail_signed_url, true); xhr.setRequestHeader('Content-Type', 'image/png'); xhr.send(image);
The file is successfully being created on Google Storage, however the content is stated as corrupt, it is indeed basically base64.
I also tried ways to convert base64 to png, yet unsuccesfully, as it was suggested here php-js-canvas example.
How can I achieve this with js and google storage ?