Create File with Google Drive Api v3 (javascript)

前端 未结 4 1037
情歌与酒
情歌与酒 2020-11-27 17:28

I want to create a file with content using Google Drive API v3. I have authenticated via OAuth and have the Drive API loaded. Statements like the following work (but produce

4条回答
  •  情歌与酒
    2020-11-27 17:49

    /*  Now to create a new file */ 
    function insertNewFile(folderId) 
    {   
        var content = " ";  
        var FolderId = ""; 
        var contentArray = new Array(content.length);
        for (var i = 0; i < contentArray.length; i++) 
        {
            contentArray[i] = content.charCodeAt(i);
        }
        var byteArray = new Uint8Array(contentArray);
        var blob = new Blob([byteArray], {type: 'text/plain'});     
        insertFile(blob, fileInserted, folderId); 
    } 
    function fileInserted(d) 
    {   
        setPercent("100");   
        var FI = FolderId;  
        if(FI !== myRootFolderId)
        {           
            insertFileIntoFolder(FI, d.id);         
            removeFileFromFolder(d.parents[0].id,d.id);     
        }   
        openFile(d.id); 
    } 
    function insertFileIntoFolder(folderId, fileId) 
    {   
        var body = {'id': folderId};   
        var request = gapi.client.drive.parents.insert({
            'fileId': fileId,
            'resource': body   });   
        request.execute(function(resp) { }); 
     } 
    

    Source: https://gist.github.com/mkaminsky11/8624150

提交回复
热议问题