Uploading Multiple Files to Google Drive with Google App Script

后端 未结 5 700
萌比男神i
萌比男神i 2020-11-27 17:57

I\'m trying to upload multiple files at once with my app. It recognizes when there are 2 or more files being selected. However, it will only upload the 1st file that is sele

5条回答
  •  -上瘾入骨i
    2020-11-27 18:11

    Updated For May 2017

    I updated and improved barragan's answer.

    Advantages:

    1. Allows users to create a subfolder name to contain all the files uploaded during this session
    2. Ensures that these subfolders all exist within one specified folder within your Google Drive
    3. The page/form is mobile-responsive

    You can start with this example just to create the script and get to know the basics.

    Then you can completely replace form.html with this:

    
    
        
            
            
            
            
            
                Send Files
            
                    
            
            
            
            
        
        
            

    This webpage allows you to send me screenshots of your dating profiles.

    1. In each of your dating apps, take a screenshot (how?) of every part of every page of your profile.
    2. Do the same for each of your photos (at full resolution).
    3. In the form below, type your first name and last initial (without any spaces or special characters), such as SallyT.
    4. Then click the first button and select all of your screenshot images (all at once).
    5. Finally, press the last button to send them all to me!
    (No spaces or special characters allowed because this will determine your folder name)

    And completely replace server.gs with this:

    function doGet() {
      var output = HtmlService.createHtmlOutputFromFile('form');
      output.addMetaTag('viewport', 'width=device-width, initial-scale=1');// See http://stackoverflow.com/a/42681526/470749
      return output.setSandboxMode(HtmlService.SandboxMode.IFRAME);
    }
    
    function uploadFileToDrive(base64Data, fileName, subfolderId) {
      Logger.log(subfolderId);
      try{
        var splitBase = base64Data.split(','),
            type = splitBase[0].split(';')[0].replace('data:','');
    
        var byteCharacters = Utilities.base64Decode(splitBase[1]);
        var ss = Utilities.newBlob(byteCharacters, type);
        ss.setName(fileName);
        var subfolder = DriveApp.getFolderById(subfolderId);
        var file = subfolder.createFile(ss);
        Logger.log(file);
        return file.getName() + ' at ' + file.getUrl();
      } catch(e){
        return 'createFile Error: ' + e.toString();
      }
    }
    
    function createSubfolder(subfolderName){
      var dropbox = subfolderName + Utilities.formatDate(new Date(), "US/Eastern", "_yyyy-MM-dd");
        Logger.log(dropbox);
        var parentFolderId = "{ID such as 0B9iQ20nrMNYAaHZxRjd}";
        var parentFolder = DriveApp.getFolderById(parentFolderId);
        var folder;
        try {
          folder = parentFolder.getFoldersByName(dropbox).next();      
        }
        catch(e) {
          folder = parentFolder.createFolder(dropbox);
        }
        Logger.log(folder);
      return folder.getId();
    }
    

提交回复
热议问题