AnsweredAssumed Answered

Subir archivos desde flex

Question asked by eduandres01_9941 on Oct 27, 2008
Latest reply on Feb 8, 2010 by lexcorp
Hola a todos!!
Estoy intentando subir archivos a Alfresco desde Flex con el siguiente web script:

<webscript>
  <shortname>upload</shortname>
  <description>Uploads file </description>
  <url>/esoft/upload</url>
  <format default="xml">argument</format>
  <authentication>user</authentication>
  <transaction>required</transaction>
</webscript>

var filename = null;
var content = null;
var mimetype = null;

var name = null;
var title = null;
var description = null;
var author = null;

var nodeid = null;

var storeid = null;
var path = null;

var thumbnailNames = null;


// locate file attributes
for each (field in formdata.fields)
{
   switch (String(field.name).toLowerCase())
   {
      case "title":
         title = field.value;
         break;
      case "desc":
         description = field.value;
         break;
      case "author":
         author = field.value;
         break;
      case "name":
         // for having name propertie different than original filename
         name = field.value;
         break;
      case "file":
         if (field.isFile)
         {
            filename = field.filename;
            content = field.content;
         }
         break;
      case "mimetype":
         mimetype = field.value;
         break;
      case "nodeid": 
         nodeid = field.value;
         break;
      case "storeid":  
         storeid = field.value;
         break;
      case "path": 
         path = field.value;
         break;
      case "thumbnails":
         thumbnailNames = field.value;
         break;        
   } 
}

var isAvmNode = false;

if ((nodeid != null) && (nodeid != ""))
{
   // get folder to upload into using doc mgt nodeid
   model.folder = search.findNode("workspace://SpacesStore/" + nodeid);
}
else if ( (storeid != null) && (storeid != "") && (path != null) && (path != "") )
{
   // get folder to upload into with avm storeid and avm path
   var store = avm.lookupStore(storeid);
   if (store != null)
   {
      var pathWithStore = storeid + ":" + path;
      model.folder = avm.lookupNode(pathWithStore);
      isAvmNode = true;
   }
}
else if ((path != null) && (path != ""))
{
   // get folder to upload into with doc mgt path
   model.folder = roothome.childByNamePath(path);
}

var version = server.version;
var versionNum = parseFloat(version);

// ensure folder and mandatory file attributes have been located
if (model.folder == null || filename == undefined || content == undefined)
{
   status.code = 400;
   status.message = "Uploaded file cannot be located in request";
   status.redirect = true;
}
else
{
   // create document in model.folder for uploaded file
   upload = model.folder.createFile("upload" + model.folder.children.length + "_" + filename);
   upload.properties.content.write(content);
  
   upload.properties.content.mimetype = mimetype;
  
   if (versionNum >= 2.9)
   {
      upload.properties.content.encoding = "UTF-8";
   }
   else
   {
      upload.properties.encoding = "UTF-8";  
   }
  
   if (isAvmNode == false)
   {
      if (name != null)
      {
        upload.properties.name = name;
      }
      else
      {
        upload.properties.name = filename;
      }

      if (title != null)
      {
        upload.properties.title = title;
      }

      if (description != null)
      {
        upload.properties.description = description;
      }

      if (author != null)
      {
        upload.properties.author = author;
      }

      upload.save();
   }
   else
   {
      // need to use rename on avm nodes
      if (name != null)
      {
        upload.rename(name);
      }
      else
      {
        upload.rename(filename);
      }

      // note setting other properties does not work currently
      // with the javascript api on avm nodes

      upload.save();
   }     

  
   if (versionNum >= 3.0)
   {
      if (thumbnailNames != null)
      {
         var thumbnails = thumbnailNames.split(",");
         for (var i = 0; i < thumbnails.length; i++)
         {
            var thumbnailName = thumbnails[i];
            if(thumbnailName != "" && thumbnailService.isThumbnailNameRegistered(thumbnailName))
            {
               upload.createThumbnail(thumbnailName, true);
            }
         }
      }
   }    
   model.upload = upload;
}


<upload>
<server>Alfresco ${server.edition} v${server.version}</server>
<folder>
   <name>${folder.name}</name>
   <id>${folder.id}</id>
   <path>${folder.displayPath}/${folder.name}</path>
</folder>
<node>
   <name>${upload.properties.name}</name>
   <id>${upload.id}</id>
   <path>${upload.displayPath}/${upload.name}</path>
   <title>${upload.properties.title!}</title>
   <description>${upload.properties.description!}</description>
   <#if upload.isDocument>
      <author>${upload.properties.author!}</author>
      <size>${upload.properties.content.size}</size>
   </#if>
</node>
</upload>

El problema es que al tratar de ejecutar el web script desde flex para subir el archivo obtengo el siguiente error:

Error: Execution of web script failed:Failed to execute script '/esoft/upload.post.js (in classpath store file:C:/Alfresco/tomcat/webapps/alfresco/WEB-INF/classes/alfresco/templates/webscripts)': Failed to execute script '/esoft/upload.post.js (in classpath store file:C:/Alfresco/tomcat/webapps/alfresco/WEB-INF/classes/alfresco/templates/webscripts)': ReferenceError: "formdata" is not defined. (AlfrescoScript#20) URL=http://localhost:8080/alfresco/service/flexspaces/uploadNew
   at org.alfresco.framework.service.webscript::WebScriptService/raiseFailureEvent()[C:\dev\WorkSpace\AlfrescoConFlex\My\org\alfresco\framework\service\webscript\WebScriptService.as:284]
   at org.alfresco.framework.service.webscript::WebScriptService/onResultEvent()[C:\dev\WorkSpace\AlfrescoConFlex\My\org\alfresco\framework\service\webscript\WebScriptService.as:251]
   at flash.events::EventDispatcher/dispatchEventFunction()
   at flash.events::EventDispatcher/dispatchEvent()
   at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent()[E:\dev\3.1.0\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:170]
   at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler()[E:\dev\3.1.0\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:193]
   at mx.rpc::Responder/result()[E:\dev\3.1.0\frameworks\projects\rpc\src\mx\rpc\Responder.as:43]
   at mx.rpc::AsyncRequest/acknowledge()[E:\dev\3.1.0\frameworks\projects\rpc\src\mx\rpc\AsyncRequest.as:74]
   at DirectHTTPMessageResponder/completeHandler()[E:\dev\3.1.0\frameworks\projects\rpc\src\mx\messaging\channels\DirectHTTPChannel.as:381]
   at flash.events::EventDispatcher/dispatchEventFunction()
   at flash.events::EventDispatcher/dispatchEvent()
   at flash.net::URLLoader/onComplete()

Dice que formdata no esta definido, no se que mas tengo que hacer para que el web script se ejecute bien.
Agradesco sus colaboraciones.
Gracias!! :D

Outcomes