Search code examples
google-apps-scriptgoogle-sites

UI to upload/attach file to Google Sites page


These are the errors I'm getting:

[13-12-23 22:43:26:376 EST] Page type: FileCabinetPage

[13-12-23 22:43:26:376 EST] File blob: undefined

[13-12-23 22:43:26:383 EST] Error msg: Cannot find method addHostedAttachment((class)).

Here's an excerpt from my code:

function doGet(){

  var app = UiApp.createApplication().setTitle("Shipping Label Request Form");
  var form = UiApp.createFormPanel();
  var panel = UiApp.createVerticalPanel();
  var attachment = app.createFileUpload().setId('attachment').setName('attachment')
  var button = app.createSubmitButton('Submit').setId("button");

  app.add(form);
  form.add(panel);
  panel.add(attachment);
  panel.add(button);

  // not needed with formPanel //
  // var handler = app.createServerHandler('submitAnnouncement'); 
  // button.addClickHandler(handler); //

  return app;
}

// submitAnnouncement changed to doPost()
function doPost(e) {

  var app = UiApp.getActiveApplication();
  var page = SitesApp.getPageByUrl('https://sites.google.com/...')
  var fileBlob = e.parameter.attachment;

  Logger.log('Page type:' +page)
  Logger.log('File blob:' +fileBlob)

  try {
  page.addHostedAttachment(fileBlob)
  }
  catch(e){
    Logger.log('Hosted attachment error msg:' +e.message);
    }
}

Solution

  • The parameter attachment will not be available to the server handler in your code, in order to fix this you can either use

    var handler = app.createServerHandler('submitAnnouncement')addCallbackElement(attachment)
    

    Or you can envelope the attachment and button in a Vertical Panel which must then be put inside a Form Panel and your code should start working.