Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 67441

Upload/Naming folders to a destination folder on Google Drive using Google Apps Script

$
0
0

I'm not skilled in coding/scripting language, I apologize in advance if I don't get the terms right. I have been trying for two days (without luck) to edit this script, which anonymously uploads files to Google Drive, but I'm running into a few problems.

  1. The script is uploading files into the default Google drive folder, instead of my desired folder which I created [ "Uploads" on the script].
  2. Can I get the script to create a folder name from the same name input on the form?
  3. Right now, even after the progress bar completes uploading, the form appears like it's not submitted, as the information is still visible in the form. After clicking "submit," is there a way to get the form to auto-refresh or a 'submission complete'?

Thank you.

<!DOCTYPE html><html><body><div id="formcontainer"><label for="myForm">2020 Vision:</label><br><br><form id="myForm"> <label for="myForm">Information:</label><div><input type="text" name="objectives" placeholder=“Objectives:”></div><div><input type="text" name="goals" placeholder=“Goals:”></div><div><label for="fileText">Remarks:</label><TEXTAREA name="projectDescription"
          placeholder="Describe your attachment(s) here:"
          style ="width:400px; height:200px;"></TEXTAREA></div> <br><label for="attachType">Choose Attachment Type:</label><br><select name="attachType"><option value="Pictures Only">Picture(s)</option><option value="Proposals Only">Documents</option><option value="Pictures & Proposals">All</option></select><br><label for="myFile">Upload Attachment(s):</label><br><input type="file" name="filename" id="myFile" multiple><input type="button" value="Submit" onclick="iteratorFileUpload()"></form></div><div id="output"></div><div id="progressbar"><div class="progress-label"></div></div><script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script><link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/themes/smoothness/jquery-ui.css"><script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.11.4/jquery-ui.min.js"></script><script>

var numUploads = {};
numUploads.done = 0;
numUploads.total = 0;
folderName;

// Upload the files into a folder in drive
// This is set to send them all to one folder (specificed in the .gs file)
function iteratorFileUpload() {
    folderName = "Batch: "+new Date();
    var allFiles = document.getElementById('myFile').files;

    if (allFiles.length == 0) {
        alert('No file selected!');
    } else {
        //Show Progress Bar

        numUploads.total = allFiles.length;
        $('#progressbar').progressbar({
        value : false
        });//.append("<div class='caption'>37%</div>");
        $(".progress-label").html('Preparing files for upload');
        // Send each file at a time
        for (var i = 0; i < allFiles.length; i++) {
            console.log(i);
            sendFileToDrive(allFiles[i]);
        }
    }
}

function sendFileToDrive(file) {
    var reader = new FileReader();
    reader.onload = function (e) {
        var content = reader.result;
        console.log('Sending ' + file.name);
        var currFolder = ‘UPLOADS’; // my desired destination folder
        google.script.run.withSuccessHandler(updateProgressbar).uploadFileToDrive(content, file.name, folderName);
    }
    reader.readAsDataURL(file);
}

function updateProgressbar( idUpdate ){
   console.log('Received: ' + idUpdate);
   numUploads.done++;
   var porc = Math.ceil((numUploads.done / numUploads.total)*100);
   $("#progressbar").progressbar({value: porc });
   $(".progress-label").text(numUploads.done +'/'+ numUploads.total);
   if( numUploads.done == numUploads.total ){
      //uploadsFinished();
      numUploads.done = 0;
   };
}</script><script>
    function fileUploaded(status) {
      document.getElementById('myForm').style.display = 'none';
      document.getElementById('output').innerHTML = status;
    }</script><style>
    body {
      max-width: 400px;
      padding: 20px;
      margin: auto;
    }
    input {
      display: inline-block;
      width: 100%;
      padding: 5px 0px 5px 5px;
      margin-bottom: 10px;
      -webkit-box-sizing: border-box;‌​ -moz-box-sizing: border-box;
      box-sizing: border-box;
    }
    select {
      margin: 5px 0px 15px 0px;
    }
    input[type="submit"] {
      width: auto !important;
      display: block !important;
    }
    input[type="file"] {
      padding: 5px 0px 15px 0px !important;
    }
#progressbar{
    width: 100%;
    text-align: center;
    overflow: hidden;
    position: relative;
    vertical-align: middle;

}
.progress-label {
      float: left;
margin-top: 5px;
      font-weight: bold;
      text-shadow: 1px 1px 0 #fff;
          width: 100%;
    height: 100%;
    position: absolute;
    vertical-align: middle;
    }
  </style></body>

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

function uploadFileToDrive(base64Data, fileName, folderName) {
  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 = folderName || "UPLOADS"; //my desired destination for uploads
    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();
  }
}

Viewing all articles
Browse latest Browse all 67441

Trending Articles