13 de julio de 2016

:: Alfresco. Crear un WebScript para subir ficheros


Para definir un WebScript que nos permita subir un fichero al repositorio, seguiremos los siguientes pasos:

.- Crearemos el fichero correspondiente al descriptor del WebScript, y lo nombraremos como: "upload.get.desc.xml" con el siguiente contenido:
< webscript >
  < shortname >Subida de ficheros. Netic360< /shortname >
  < description >Formulario para subida de ficheros de contenido y metadaros al repositorio< /description >
  < url>/netic360/upload< /url >
  < authentication >user< /authentication >
< /webscript >

El fichero anterior lo copiaremos en: "Repositorio>Diccionario de datos>Web Scripts"

.- Crearemos el fichero correspondiente a la plantilla o vista del WebScript, y lo nombraremos como: "upload.get.html.ftl"
El contenido de este fichero será el siguiente:

< html >
 < head >
   < title >Upload Web Script Sample< /title >
   < link rel="stylesheet" href="${url.context}/css/main.css" TYPE="text/css">
 < /head >
 < body >
   < table >
     < tr >
       < td >Alfresco

       < td >WebScript para upload de ficheros
     < /tr >
     < tr >< td >< td >Alfresco ${server.edition} v${server.version}
   < /table >
   < p >
   < table >
    
       < tr >< td >File:< /td >< td >< input type="file" name="file" >< /td >< /tr >
       < tr >< td >Title:< /td >< td >< input name="title" >< /td >< /tr >
       < tr >< td >Description:< /td >< td >< input name="desc" >< /td >< /tr >
       < tr >< td >

       < tr >< td >< input type="submit" name="submit" value="Upload" >< /td >< /tr >
     < /form >
   < /table >
 < /body >
< /html >

.- Crearemos el fichero correspondiente al descriptor del WebScript para el método "POST". El fichero lo llamaremos: "upload.post.desc.xml"
Este fichero tendrá el siguiente contenido:
< webscript >
  < shortname >File Upload Sample< /shortname >
  < description >Upload file content and meta-data into Repository< /description >
  < url >/netic360/upload< /url >
  < authentication >user< /authentication >
< /webscript >

.- Crearemos el fichero correspondiente al script a ejecutar, y lo llamaremos: "upload.post.js"
El contenido de este fichero será el siguiente:

var filename = null;
var content = null;
var title = "";
var description = "";

// locate file attributes
for each (field in formdata.fields)
{
  if (field.name == "title")
  {
    title = field.value;
  }
  else if (field.name == "desc")
  {
    description = field.value;
  }
  else if (field.name == "file" && field.isFile)
  {
    filename = field.filename;
    content = field.content;
  }
}

// ensure mandatory file attributes have been located
if (filename == undefined || content == undefined)
{
  status.code = 400;
  status.message = "Uploaded file cannot be located in request";
  status.redirect = true;
}
else
{
  // create document in company home for uploaded file
  upload = companyhome.createFile("upload" + companyhome.children.length + "_" + filename) ;
 
  upload.properties.content.write(content);
  upload.properties.content.setEncoding("UTF-8");
  upload.properties.content.guessMimetype(filename);
 
  upload.properties.title = title;
  upload.properties.description = description;
  upload.save();

  // setup model for response template
  model.upload = upload;
}

.- Por último crearemos el fichero correspondiente a la vista tras subir el fichero. El nombre del fichero será: "upload.post.html.ftl"
Este tendrá el siguiente contenido:
< html >
 < head >
   < title >Upload Web Script Sample< /title >
   < link rel="stylesheet" href="${url.context}/css/main.css" TYPE="text/css" >
 < /head >
 < body >
   < table >
     < tr >
       < td >< img src="${url.context}/images/logo/AlfrescoLogo32.png" alt="Alfresco" />< /td >
       < td >< nobr >Upload Web Script Sample< /nobr >< /td >
     < /tr >
     < tr >< td >Alfresco ${server.edition} v${server.version}< /td >< /tr >
     < tr >
< /td >< /tr >
     < tr >< td >Uploaded < a href="${url.serviceContext}/sample/folder${upload.displayPath}" >${upload.name}< /a > of size ${upload.properties.content.size}.< /td >< /tr >
   < /table >
 < /body >
< /html >

.- Para invocar al webscript insertaremos en el navegador la siguiente url:
localhost:8080/alfresco/service/netic360/upload

Más información: http://docs.alfresco.com/5.0/concepts/ws-webscripts.html

No hay comentarios:

Publicar un comentario

Déjanos tu comentario

:: Liferay. Script para ir al inicio de la página

En diversos temas de apariencia es necesario contar un enlace que permita al usuario realizar un scroll al inicio de la página. Este comport...