title: Uploading files on Google Sites - file cabinet, list page - doPost issues, unexpected error
body: <p>I had this script working, but a few weeks ago it suddenly stopped - I assumed I made some programming error but I've been unable to debug this for weeks.</p>
  
  <p>I want a script on google sites that allows someone to upload a file (and some other information, like its name) and have the script attach it to a new file cabinet page and then update a list page with the new page's information.</p>
  
  <p>I used parts of code from an old forum post, updating where I needed to. I am not opposed to rewriting if someone knows of a simpler way to do this.</p>
  
  <p>Thanks!</p>
  
  <p>EDIT: the problem seems to be in the doPost(e) function - after hitting submit, nothing happens, the gadget goes blank and I get the error message: "An unexpected error occurred."</p>
  
  <pre><code>function doGet(e){
    return getUi(e);
  }
  
  function getUi(e) {
    //create user interface for uploading information
    var app = UiApp.createApplication().setTitle('File Upload');
  
    var form = app.createFormPanel().setId('form').setEncoding('multipart/form-data');
  
    //create grid to store information
    var formContent = app.createGrid().resize(6,2);
  
    formContent.setText(0,0,'File Name: ');
    var fileName = app.createTextBox().setName('fileName').setId('fileName');
    formContent.setWidget(0,1,fileName);
  
    var fileUpload = app.createFileUpload().setName('fileUpload').setId('fileUpload');
    formContent.setWidget(1,1,fileUpload);
  
    formContent.setWidget(2,0,app.createLabel('Short Description: '));
    var fileDescription = app.createTextBox().setName('fileDescription').setId('fileDescription');
    formContent.setWidget(2,1,fileDescription);
  
    formContent.setText(3,0,'Level: ');
    var level = app.createListBox().setName('level').setId('level').addItem('Introductory').addItem('Intermediate').addItem('Advanced');
    formContent.setWidget(3,1,level);
  
    formContent.setText(4,0,'Subject: ');
    var subject = app.createListBox().setName('subject').setId('subject').addItem('Fundamentals').addItem('Observing').addItem('Solar System').addItem('Stars').addItem('Stellar Evolution').addItem('Milky Way').addItem('Galaxies').addItem('Cosmology')
    formContent.setWidget(4,1,subject);
  
    var submitButton = app.createSubmitButton("Submit").setId("submitButton");
    formContent.setWidget(5,1,submitButton);
    //var handler = app.createServerHandler("pauseUi");
    //submitButton.addClickHandler(handler);
  
    form.add(formContent);
    app.add(form);
  
    var resultTextArea = app.createTextArea().setName("resultArea").setId("resultArea");
    resultTextArea.setVisibleLines(5);
    resultTextArea.setSize("80%","80%");
    app.add(resultTextArea);
    var ogga = 'good god';
    Logger.log(ogga);
    return app; 
  }
  
  
  function pauseUi(e){
    var app = UiApp.getActiveApplication();
    app.getElementById("file").setEnabled(false);
    app.getElementById("fileName").setEnabled(false);
    app.getElementById("fileDescription").setEnabled(false);
    app.getElementById("submitButton").setEnabled(false);
    app.getElementById("level").setEnabled(false);
    app.getElementById("subject").setEnabled(false);
    app.getElementById("resultArea").setEnabled(false);
    app.getElementById("resultArea").setText('Please wait...'); 
    return app;
  }
  
  function resetUi(app){
    app.getElementById("file").setEnabled(true);
    app.getElementById("fileName").setEnabled(true);
    app.getElementById("fileDescription").setEnabled(true);
    app.getElementById("submitButton").setEnabled(true);
    app.getElementById("level").setEnabled(true);
    app.getElementById("subject").setEnabled(true);
    app.getElementById("resultArea").setEnabled(true);
  }
  
  function doPost(e){
    var app = UiApp.getActiveApplication();
    Logger.log(e.parameter);
    //create new page for uploaded file
    var msg = createNewPage(e.parameter.fileName,e.parameter.fileUpload,e.parameter.fileDescription,e.parameter.level,e.parameter.subject);  
    //reset upload page
    resetUi(app);
    //notify user of success or failure
    app.getElementById("resultArea").setText(msg.join('\n'));
    return app;
  }
  
  function createNewPage(fileName,uploadedFile,fileDescription,level,subject){
    //site information
    var mysite = SitesApp.getSite("uw.edu","introductory-astronomy-clearinghouse");
    var labsPage = mysite.getChildByName("labs-exercises");
    Logger.log('url for lab page is: '+labsPage.getUrl());
    var msg = [];
    //create new page
    var page = findPage(fileName);
    if (page == null){
      var page = labsPage.createFileCabinetPage(fileName, normalizeName(fileName), '');
    }
  
    try{
      //add file to file cabinet
      var attachment = page.addHostedAttachment(uploadedFile);
      //see if already listed on list
      var listItem = getFileListItem(fileName);
      if (listItem == null) {
        var fileUrl = page.getUrl();
        var newValues = [repairUrlValue(fileUrl, fileName), fileDescription, level, subject];
        labsPage.addListItem(newValues);
      }
      msg.push('Refresh page to see your item added or edit your file at: '+fileUrl);
    } catch(e){
      msg.push('Failed to upload the '+ fileName +' attachment, possibly due to size constraints or time out issues.');
    }
    return msg; 
  }
  
  function getFileListItem(fileName){
    var mysite = SitesApp.getSite("uw.edu","introductory-astronomy-clearinghouse");
    var labPage = mysite.getChildByName("labs-exercises");
  
    var i = getListItemIndex(labPage,fileName,0);
  
    var items = labPage.getListItems();
    Logger.log('labPage list item'+items[i].getValueByIndex(0));
    return (i&gt;-1)? labPage.getListItems()[i]:null;
  }
  
  //get the index of your item
  
  function getListItemIndex(listPage,propOneValue,propOneIndex){
    //for column propOneIndex, returns index (row) that equals propOneValue, or -1.
    var listItems = listPage.getListItems();
    Logger.log('listItems '+listItems.length);
    var r = -1;
    for (var i = 0;i&lt;listItems.length &amp;&amp; r&lt;0;i++){
      if ((listItems[i].getValueByIndex(propOneIndex) == propOneValue)){r = i;}
    }
    return r;
  }
  
  
  
  //repairs URL
  
  function repairUrlValue(url,text){
    Logger.log('buildAnchor for'+text+'-&gt;'+url);
    var a = '&lt;a href="'+url+'"&gt;"'+text+'&lt;/a&gt;';
    Logger.log('...built '+a);
    return a;
  }
  
  //find name of page with normalized name
  
  function findPage(name){
    var mysite = SitesApp.getSite("uw.edu","introductory-astronomy-clearinghouse");
    var parentPage = mysite.getChildByName("labs-exercises");
    var norm = normalizeName(name);
  
    var page = parentPage.getChildByName(norm);
    Logger.log('findPage '+page.getUrl());
    return page; 
  }
  
  //get size of attached file in KB
  
  function getBlobSize(blob) {
    return Math.round(blob.getBytes().length/1024)+" KB"; 
  }
  
  //put the name into reasonable form
  
  function normalizeName(header){
    var key = "";
    for (var i = 0; i &lt; header.length; ++i){
      var letter = header[i];
      if (letter == " " &amp;&amp; key.length &gt; 0){
        key +='-';
        continue;
      }
      if (!isAlnum(letter)){
        continue;
      }
      if (key.length == 0 &amp;&amp; isDigit(letter)){
        continue; //first character must be a letter
      }
      key += letter.toLowerCase();
    }
    Logger.log("normalizeName from '"+header+"' to '"+key+"'");
    return key;
  }
  
  /*
   i s A l n u m
  Returns true if the character char is alphabetical, false otherwise
  */
  
  function isAlnum(char){
    return char &gt;= 'A' &amp;&amp; char &lt;= 'Z' ||
      char &gt;='a' &amp;&amp; char &lt;= 'z' ||
      isDigit(char);
  }
  
  /* i s D i g i t
  Returns true if the character is a digit, false otherwise.
  */
  
  function isDigit(char){
    return char &gt;='0' &amp;&amp; char &lt;= '9';
  }
  </code></pre>