2017-06-12 123 views
0

我开发了一个连接到Google Drive的Web表单,并允许将文件上传到特定的文件夹。当前代码仅上传所选的第一个文件,我希望能够上传所选的所有文件。我目前的代码发布在下面。有什么建议么?如何更改我的Google Apps脚本代码以允许将多个文件上传到Google云端硬盘?

的index.html

<body> 
 

 
    <h2 class="col-sm-offset-4 col-sm-8" style="padding-bottom: 50px;">Smart-HR Connect Data Gather</h2> 
 

 
    <form class="form-horizontal" id="genericForm"> 
 

 

 
    <div class="form-group" id="show-me6" style="display: none;"> 
 
     <label class="col-sm-3 control-label">Please upload your Chart of Payroll Accounts &amp; your Last Journal Entry:</label> 
 
     <div> 
 
     <input name="chartAccJournalEntry" type="file" id="file" multiple> 
 
     </div><br><br> 
 

 
    </div> 
 

 

 
    <input class="col-sm-offset-3" type="submit" onclick="this.value='Thank you ...'; google.script.run.withSuccessHandler(formSubmitted) .writeForm(this.parentNode); return false;"> 
 

 
    </form> 
 

 

 

 
    <div id="output"></div> 
 
    <!-- where the confirmation message goes --> 
 
    
 
    <script> 
 
    var file, 
 
     reader = new FileReader(); 
 

 
    // Upload the file to Google Drive 
 

 

 
    reader.onloadend = function(e) { 
 
     google.script.run 
 
     .withSuccessHandler(showMessage) 
 
     .uploadFileToGoogleDrive(
 
      e.target.result, file.name, 
 
      $('input#distinguishedName').val() 
 
     ); 
 
    }; 
 

 
    function showMessage(e) { 
 
     $('#progress').html(e); 
 
    } 
 

 
    function formSubmitted(status) { 
 
     document.getElementById('genericForm').style.display = 'none'; //matches your form name or whatever you want to disappear post-submission 
 
     document.getElementById('output').innerHTML = status; //displays in item with the 'output' id 
 
     file = $('#file')[0].files[0]; 
 
     reader.readAsDataURL(file); 
 
     showMessage("Uploading file.."); 
 

 

 
    } 
 
    </script> 
 
</body>

Code.gs

//basic function that builds the form using index.html as the template 
 
function doGet(e) { 
 
    return HtmlService 
 
    .createTemplateFromFile('index') 
 
    .evaluate() 
 
    .setTitle("Smart-HR Connect Data Gather") 
 
    .setSandboxMode(HtmlService.SandboxMode.NATIVE); 
 
} 
 
    
 
function uploadFileToGoogleDrive(data, file, name, email) { 
 

 
    try { 
 

 
    var submissions = "Data Gather File Uploads"; 
 
    var folder, folders = DriveApp.getFoldersByName(submissions); 
 

 
    if (folders.hasNext()) { 
 
     folder = folders.next(); 
 
    } else { 
 
     folder = DriveApp.createFolder(submissions); 
 
    } 
 

 
    var contentType = data.substring(5,data.indexOf(';')), 
 
     bytes = Utilities.base64Decode(data.substr(data.indexOf('base64,')+7)), 
 
     blob = Utilities.newBlob(bytes, contentType, file); 
 

 
    folder.createFolder([name, email].join(" ")).createFile(blob); 
 

 
    return "OK"; 
 

 
    } catch (f) { 
 
    return f.toString(); 
 
    } 
 

 
}

回答

0

您可以参考本教程Multiple File Upload to Google Drive Using Google Script

function doGet() { 
    return HtmlService.createHtmlOutputFromFile('form') 
    .setSandboxMode(HtmlService.SandboxMode.IFRAME); 
} 


function uploadFileToDrive(base64Data, fileName, dropbox) { 
    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 dropbox = "WTELNSD2016"; // Folder Name 
    var folder, folders = DriveApp.getFoldersByName(dropbox); 

    if (folders.hasNext()) { 
     folder = folders.next(); 
    } else { 
     folder = DriveApp.createFolder(dropbox); 
    } 
    var file = folder.createFile(ss); 
    return file.getName(); 
    }catch(e){ 
    return 'Error: ' + e.toString(); 
    } 
} 

在本SO thread所述,多属性只能在IFRAME模式,但文件输入在IFRAME模式打破。

希望这会有所帮助。

相关问题