Reputation: 1280
I can't find the answer in the Google Apps Script Reference, so I'm asking: Can you pass a blob through a .run() function in a google script. My code is as follows.
This is my HTML file's script. All the HTML contains is a object. And a
to make "print" statements.
<script>
var canvas = document.getElementById('myCanvas');
var can = canvas.getContext("2d");
can.fillRect(20,20,150,100);
var canvasData = canvas.toDataURL('image/png', 1);
var blob = toBlob(canvasData);
document.getElementById('ins').innerHTML = "Got the Blob " + blob.type;
google.script.run.printCanvas(blob);
function toBlob(dataURI) {
// convert base64 to raw binary data held in a string
// doesn't handle URLEncoded DataURIs - see SO answer #6850276 for code that does this
document.getElementById('ins').innerHTML = ("Data URI:" + dataURI);
var byteString = atob(dataURI.split(',')[1]);
// separate out the mime component
var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0]
// write the bytes of the string to an ArrayBuffer
var ab = new ArrayBuffer(byteString.length);
var ia = new Uint8Array(ab);
for (var i = 0; i < byteString.length; i++) {
ia[i] = byteString.charCodeAt(i);
}
// write the ArrayBuffer to a blob, and you're done
var blob = new Blob([ab], {type: 'image/png'});
return blob;
}
</script>
The "print" of blob.type, gives me "Got the Blob image/png"
However, the app won't execute the .run(blob), error code:
"Uncaught TypeError: Failed due to illegal value in property: 0"
I know that GAS won't pass DIVs, so I was wondering if it is just impossible to pass a blob to the .gs file. I can't figure out why else the application won't run. If it is possible, tell me how, please.
I know it's not an the .gs side, because the first line of the .gs is a DocumentApp.alert, which doesn't pop. So, the application is not reaching the .gs file.
Upvotes: 3
Views: 2215
Reputation: 1280
So, thank you to Sandy Good. The answer is in-fact, NO. It is impossible to send a blob to the .gs page as described.
However, you can encode your blob into base-64 encoded string,
https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL
And then decode it in the .gs file with Utilities.base64Decode(base64encodedstring);
Upvotes: 1