2016-01-20 211 views
0

截至目前,代碼正在將文件上傳到「something」文件夾中。在Google Apps腳本上創建上傳文件的文件夾

我試圖讓每次上傳一組文件時,它們都會進入它們自己單獨的文件夾內的主上傳文件夾。

我也試圖設置一個返回來顯示完成上傳的確認,但這些文件只是簡單地上傳而後沒有任何反應。由於

form.html

<body> 
    <div id="formcontainer"> 

    <label for="myForm">Project Database Attachment Uploader:</label> 

    <br><br> 


    <form id="myForm"> 
     <label for="myForm">Project Details:</label> 
     <div> 
     <input type="text" name="zone" placeholder="Zone:"> 
     </div> 
     <div> 
     <input type="text" name="building" placeholder="Building(s):"> 
     </div> 
     <div> 
     <input type="text" name="propertyAddress" placeholder="Property Address:"> 
     </div> 
     <div> 

     <label for="fileText">Project Description:</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">Proposal(s)</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; 

// 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() { 
    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 = 'Something'; 
     google.script.run.withSuccessHandler(updateProgressbar).uploadFileToDrive(content, file.name, currFolder); 
    } 
    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> 

code.gs

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

function uploadFileToDrive(base64Data, fileName) { 
    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 = "Something"; // 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(); 
    } 
} 

回答

0

這裏有一些變化上傳到每批唯一的文件夾。每次成功上載批次中的文件時,都會調用您的功能 updateProgressbar。您有回調參數idUpdate實際上是每個服務器功能uploadFileToDrive上傳文件的名稱。

code.gs

function uploadFileToDrive(base64Data, fileName, folderName) { 
. 
. 
. 
var dropbox = folderName || "Some default Folder Name"; // Folder Name 
. 
. 
. 
} 

的Index.html

<script> 

var numUploads = {}, 
numUploads.done = 0, 
numUploads.total = 0, 
currFolderName; 

function iteratorFileUpload() { 
    currFolderName = "Batch: "+new Date(); // or however you want to name the batch folder 
    . 
    . 
    . 
} 



function sendFileToDrive(file) { 
. 
. 
google.script.run.withSuccessHandler(updateProgressbar).uploadFileToDrive(content, file.name, currFolderName); 
. 
. 
. 
} 

</script 
+0

斯賓塞嗨,我將如何讓這個文件的每一批在一個文件夾中保存。 示例:BatchA進入文件夾A. B批進入文件夾B. 現在每個文件都有自己的文件夾。謝謝 – bobatapioca

+0

嗨斯賓塞我有另一個問題,我試圖讓所有的上傳文件夾進入一個主文件夾。我將如何使用以下命令將它引導到該文件夾​​:var dropbox = folderName || 「一些默認的文件夾名稱」; – bobatapioca

相關問題