代码之家  ›  专栏  ›  技术社区  ›  Jim

如何使用App Script通过Docs Add-On将文件添加到Google Drive?[副本]

  •  1
  • Jim  · 技术社区  · 9 年前

    这是我的设想。我为谷歌文档创建了一个附加组件,它充当视频工具箱。

    我试图添加的一个功能是使用内置网络摄像头(使用videojs记录器)录制视频,然后在文档中链接到该视频。我已经完成了视频部分的工作,但不知道如何将webmJSBlob转换为GoogleBlob,以便我可以在用户GoogleDrive上创建一个文件,用于共享和链接。

    只是为了弄清楚这可能是如何工作的,这是我迄今为止所做的,没有任何运气。

    客户端代码

    //event handler for video recording finish   
    vidrecorder.on('finishRecord', function()
    {
        // the blob object contains the recorded data that
        // can be downloaded by the user, stored on server etc.
        console.log('finished recording: ', vidrecorder.recordedData);
        google.script.run.withSuccessHandler(function(){
            console.log("winning");
        }).saveBlob(vidrecorder.recordedData);
    });
    

    服务器端代码

    function saveBlob(blob) {
        Logger.log("Uploaded %s of type %s and size %s.",
                blob.name,
                blob.size, 
                blob.type);
    }
    

    我得到的错误似乎与blob的序列化有关。但实际上,这些异常并不是很有用——只是指向一些最小化的代码。

    EDIT:注意这里没有涉及FORM对象,因此没有表单POST,也没有FileUpload对象,正如其他人所指出的 this might be a duplicate ,但是它有点不同,因为我们得到了一个Blob对象,需要将其保存到服务器。

    1 回复  |  直到 8 年前
        1
  •  0
  •   Jim    9 年前

    感谢Zig Mandel和Steve Webster,他们从 G+ discussion 关于这一点。

    我终于拼凑出了足够多的碎片来让这个工作。

    客户端代码

            vidrecorder.on('finishRecord', function()
            {
                // the blob object contains the recorded data that
                // can be downloaded by the 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);
                };
    
            });
    

    服务器代码

    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 to raw binary data held in a string
        var byteString;
        if (dataURI.split(',')[0].indexOf('base64') >= 0)
            byteString = Utilities.base64Decode(dataURI.split(',')[1]);
        else
            byteString = decodeURI(dataURI.split(',')[1]);
    
        // separate out the mime component
        var mimeString = dataURI.split(',')[0].split(':')[1].split(';')[0];
    
        return Utilities.newBlob(byteString, mimeString, "video.webm");
    }