Question

I'm trying to figure out the flow of using google's scripts to copy images from a user's hard drive to a public folder on their Google Drive. (see https://developers.google.com/apps-script/reference/utilities/utilities#unzip%28BlobSource%29 ) Question is, do I have to write a google script that I publish as a web app from script.google.com, or can I have the script inside the javascript on the client's browser? Google has a sample of uploading images one at a time: "developers.google.com/drive/web/quickstart/quickstart-js"

I would like to upload one zipped file of images, unzip them and then reduce the size before they are stored in the user's Google Drive.

Here is some code that unzips files, but it looks like they are running this from script.google.com; it does not work: (http://webcache.googleusercontent.com/search?q=cache:NsTvlj17H4MJ:ctrlq.org/code/19506-google-drive-hosting&client=firefox-a&hs=ZEF&hl=en&gl=us&strip=1)

Was it helpful?

Solution

This is a script that I modified for another user that allows for multiple files (validation could probably limit file types to images) to be uploaded from the user's hard drive to a specific folder. The folder would be set to share publicly. You would simply change the folderID string to the string that matches the folder where you wanted to files to arrive. Put this script in a Google Sites page, and change the id in the doPost(e) function, and it should do what you want it to do. I'm not sure about the zipping and unzipping. You would publish the script in a google site as a public webapp widget.

You can see the UiApp interface here, but if you try to upload something, you will get an error because I've removed the folderId link to my drive since I put this answer live. If you need more explanation about how or why it works, let me know. Use the + and - buttons to add more files to the upload, or remove a file that you don't want to include. The files can be zips or any file type, but there isn't code included to unzip anything after it's uploaded.

//modified from script found here http://www.googleappsscript.org/miscellaneous/creating-form-elements-dynamically-using-google-apps-script-gas
//additional help from Serge to fix an error in my original code.

function doGet() {
  var app = UiApp.createApplication();
  var panel = app.createVerticalPanel();
  var formPanel = app.createFormPanel();
  var instructionsLabel = app.createLabel('Put your instructions here');
  var filesLabel = app.createLabel('Add Files to Upload');
  var table = app.createFlexTable().setId('table').setTag('0'); //Here tag will count the number of files
  //Write the header for the table
  var header = app.createLabel('File(s)');
  table.setWidget(0, 0, header);

  //Add the first row of files
  addFirstRow(app);
  var hidden = app.createHidden().setId('hiddenRowHolder').setName('hidden').setValue(table.getTag());
  //Add a button to submit the info
  var button = app.createSubmitButton('Upload File(s)');
  panel.add(instructionsLabel).add(filesLabel).add(table).add(hidden).add(button);
  formPanel.add(panel);
  app.add(formPanel);
  return app;
}

function addFirstRow(app){
  var table = app.getElementById('table');
  var tag = parseInt(table.getTag());
  Logger.log(tag);
  var numRows = tag+1;
  if(numRows >1){
    table.removeCell(numRows-1, 5);
    table.removeCell(numRows-1, 4);
  }
  Logger.log(numRows);
  var uploadWidget = app.createFileUpload();
  table.setWidget(numRows, 0, uploadWidget);
  table.setTag(numRows.toString());
  addButtons(app);
}

function addButtons(app){
  var table = app.getElementById('table');
  var numRows = parseInt(table.getTag());


  //Create handler to add/remove row
  var addRemoveRowHandler = app.createServerHandler('addRemoveRow');
  addRemoveRowHandler.addCallbackElement(table);

  //Add row button and handler
  var addRowBtn = app.createButton('+').setId('addOne').setTitle('Add row');
  table.setWidget(numRows, 4, addRowBtn);
  addRowBtn.addMouseUpHandler(addRemoveRowHandler);

  //remove row button and handler
  var removeRowBtn = app.createButton('-').setId('removeOne').setTitle('Remove row');
  table.setWidget(numRows, 5, removeRowBtn);
  removeRowBtn.addMouseUpHandler(addRemoveRowHandler);
}

function addRemoveRow(e){
  Logger.log(e.parameter.source);
  var app = UiApp.getActiveApplication();
  var table = app.getElementById('table');
  var tag = parseInt(e.parameter.table_tag);
  var hidden = app.getElementById('hiddenRowHolder');
  var source = e.parameter.source;
  //Logger.log(tag);
  if(source == 'addOne'){
    table.setTag(tag.toString());
    hidden.setValue(tag+1);
    addFirstRow(app);
  }
  else if(source == 'removeOne'){
    if(tag > 1){
      //Dcrement the tag by one
      var numRows = tag-1;
      table.removeRow(tag);
      //Set the new tag of the table
      table.setTag(numRows);
      hidden.setValue(numRows);
      //Add buttons in previous row
      addButtons(app); 
    }
  }
  return app;
}

function doPost(e) {
  var numFiles = Number(e.parameter.hidden);
  Logger.log(numFiles);
  for (var i = 1; i<=numFiles; i++){
    var fileBlob = e.parameter['file'+i];
    var newFile = DocsList.getFolderById("YOUR FILE FOLDER ID").createFile(fileBlob);//replace string with folder id where you want to place your files
  }
  var app = UiApp.getActiveApplication();
  var label = app.createLabel(numFiles +' file(s) uploaded successfully');
  app.add(label);
  return app;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top