How to Add a file to Google Drive via Docs Add On using App Script? -
this question has answer here:
here scenario. i've created add-on google docs acts video toolbox.
a feature i'm trying add ability record video using built in web cam (using videojs-recorder) , link video within doc. i've got video part working, not sure how webm js blob converted google blob can create file on users google drive sharing , linking.
just figure out how might work i've done far without luck.
client side code
//event handler video recording finish vidrecorder.on('finishrecord', function() { // blob object contains recorded data // can downloaded user, stored on server etc. console.log('finished recording: ', vidrecorder.recordeddata); google.script.run.withsuccesshandler(function(){ console.log("winning"); }).saveblob(vidrecorder.recordeddata); });
server side code
function saveblob(blob) { logger.log("uploaded %s of type %s , size %s.", blob.name, blob.size, blob.type); }
the errors seem related serialization of blob. exceptions aren't useful - , point minimized code.
edit: note there no form object involved here, hence no form post, , no fileupload objects, others have indicated this might duplicate, it's different in getting blob object , need save server.
thanks goes zig mandel , steve webster provided insight g+ discussion regarding this.
i pieced enough bits working.
client code
vidrecorder.on('finishrecord', function() { // blob object contains recorded data // can downloaded user, stored on server etc. console.log('finished recording: ', vidrecorder.recordeddata.video); var blob = vidrecorder.recordeddata.video; var reader = new window.filereader(); reader.readasdataurl(blob); reader.onloadend = function() { b64blob = reader.result; google.script.run.withsuccesshandler(function(state){ console.log("winning: ", state); }).saveb64blob(b64blob); }; });
server code
function saveb64blob(b64blob) { var success = { success: false, url: null}; logger.log("got blob: %s", b64blob); try { var blob = datauritoblob(b64blob); logger.log("gblob: %s", blob); var file = driveapp.createfile(blob); file.setsharing(driveapp.access.anyone_with_link, driveapp.permission.comment); success = { success: true, url: file.geturl() }; } catch (error) { logger.log("error: %s", error); } return success; } function datauritoblob(datauri) { // convert base64/urlencoded data component raw binary data held in string var bytestring; if (datauri.split(',')[0].indexof('base64') >= 0) bytestring = utilities.base64decode(datauri.split(',')[1]); else bytestring = decodeuri(datauri.split(',')[1]); // separate out mime component var mimestring = datauri.split(',')[0].split(':')[1].split(';')[0]; return utilities.newblob(bytestring, mimestring, "video.webm"); }
Comments
Post a Comment