我可以在浏览器客户端上使用JavaScript解压缩JavaScript吗?

时间:2014-03-11 03:46:29

标签: javascript google-apps-script unzip utilities

我正在试图找出使用谷歌脚本将图像从用户的硬盘驱动器复制到其Google云端硬盘上的公用文件夹的流程。 (请参阅https://developers.google.com/apps-script/reference/utilities/utilities#unzip%28BlobSource%29)问题是,我是否必须编写一个我从script.google.com发布为网络应用的google脚本,或者我可以在客户端浏览器的javascript中创建脚本吗? Google提供了一次上传图片的示例:“developers.google.com/drive/web/quickstart/quickstart-js”

我想上传一个压缩的图片文件,解压缩然后缩小尺寸,然后再存储在用户的Google云端硬盘中。

以下是一些解压缩文件的代码,但看起来它们是从script.google.com运行的;它不起作用:(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

1 个答案:

答案 0 :(得分:0)

This是我为另一个用户修改的脚本,允许将多个文件(验证可能限制文件类型限制为图像)从用户的硬盘驱动器上传到特定文件夹。该文件夹将设置为公开共享。您只需将folderID字符串更改为与您希望文件到达的文件夹匹配的字符串。将此脚本放在Google协作平台页面中,并更改doPost(e)函数中的id,它应该按照您希望的方式执行。我不确定拉链和解压缩。您可以将该脚本作为公共Web应用小部件在Google网站上发布。

您可以看到UiApp界面here,但是如果您尝试上传某些内容,则会收到错误,因为我已经删除了我的驱动器的folderId链接,因为我把这个答案现场直播。如果您需要更多关于它如何或为何起作用的解释,请告诉我。使用+和 - 按钮向上载添加更多文件,或删除不想包含的文件。这些文件可以是拉链或任何文件类型,但是在上传后没有包含任何解压缩代码的代码。

//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;
}