AnsweredAssumed Answered

Catch formdata response in simpledialog

Question asked by jdupont on Aug 29, 2012
Latest reply on Sep 11, 2012 by simas
Hi,

I try to create a dialog for file uploading.
My webscript work fine, the file is created.
Just one thing, i'm not able to enter in a "onSuccess", or "onFailure" method, how can i intercept a success event ?


Here is the commented client side javascript where i create the simpledialog
file : toolbar.js
    onExampleFileUpload: function DLTB_onExampleFileUpload(e, p_obj)
    {
        //get the noderef of the current folder
       var destination =  this.doclistMetadata.parent.nodeRef;

                //this url call the upload-content.post.js webscript
      var actionUrl = YAHOO.lang.substitute(Alfresco.constants.PROXY_URI + "example/upload-content/{uri}",
         {
            uri: this.doclistMetadata.parent.nodeRef.replace(":/", "")
         });
       
                //make the filedata a mandatory field
      var doSetupFormsValidation = function DLTB_oNF_doSetupFormsValidation(p_form)
      {
         // Validation
         // filedata: mandatory value
         p_form.addValidation(this.id + "-uploadContent-filedata", Alfresco.forms.validation.mandatory, null, "change");
         p_form.setShowSubmitStateDynamically(true, false);
      };
      
      this.modules.uploadContent = new Alfresco.module.SimpleDialog(this.id + "-uploadContent").setOptions(
            {
               width: "30em",
                                        //call the uploadexamplecontent.get.html.ftl webscript wich return the html form
               templateUrl: Alfresco.constants.URL_SERVICECONTEXT + "modules/documentlibrary/upload-examplecontent",
               actionUrl: actionUrl,
               doSetupFormsValidation:
               {
                  fn: doSetupFormsValidation,
                  scope: this
               },
               firstFocus: this.id + "-uploadContent-title",
                                        // I need to go in this method
               onSuccess:
               {
                  fn: function DLTB_onUploadContent_callback(response)
                  {
                     var node = response.json.item;
                     YAHOO.Bubbling.fire("nodeCreated",
                     {
                        name: node.name,
                        parentNodeRef: destination,
                        highlightFile: node.name
                     });
                     Alfresco.util.PopupManager.displayMessage(
                     {
                        text: this.msg("message.new-content.success", node.name)
                     });
                  },
                  scope: this
               },
                                        //this one too, but less often
               onFailure:
               {
                  fn: function DLTB_onUploadContent_failure(response)
                  {
                     Alfresco.util.PopupManager.displayMessage(
                     {
                        text: this.msg("message.new-content.failure")
                     });
                  },
                  scope: this
               }
            });

      this.modules.uploadContent.show();
      
    }

The form
file : uploadexamplecontent.get.html.ftl

<#assign el=args.htmlid?html>
<div id="${el}-dialog" class="upload-content">
   <div id="${el}-dialogTitle" class="hd">${msg("title")}</div>
   <div class="bd">
      <form id="${el}-form" action="" method="post" enctype="multipart/form-data" accept-charset="utf-8">
         <div class="yui-g">
            <h2 id="${el}-dialogHeader">${msg("header")}</h2>
         </div>
         <div class="yui-gd">
            <div class="yui-u first"><label for="${el}-title">${msg("label.title")}:</label></div>
            <div class="yui-u"><input id="${el}-title" type="text" name="title" tabindex="0" /></div>
         </div>
         <div class="yui-gd">
            <div class="yui-u first"><label for="${el}-description">${msg("label.description")}:</label></div>
            <div class="yui-u"><textarea id="${el}-description" name="description" rows="3" cols="20" tabindex="0" ></textarea></div>
         </div>
         <div class="yui-gd">
            <div class="yui-u first"><label for="${el}-filedata">${msg("label.filedata")}:</label></div>
            <div class="yui-u"><input type="file" id="${el}-filedata" name="filedata" tabindex="0" /></div>
         </div>
         <div class="bdft">
            <input type="submit" id="${el}-ok" value="${msg("button.ok")}" tabindex="0" />
            <input type="button" id="${el}-cancel" value="${msg("button.cancel")}" tabindex="0" />
         </div>
      </form>
   </div>
</div>

the server side webscript which create the node
file : upload-content.post.js

<import resource="classpath:/alfresco/templates/webscripts/org/alfresco/slingshot/documentlibrary/parse-args.lib.js">

function exitUpload(statusCode, statusMsg)
{
   status.code = statusCode;
   status.message = statusMsg;
   status.redirect = true;
   formdata.cleanup();
}

function main()
{
   try
   {
      // Use helper function to get the arguments
      var parsedArgs = ParseArgs.getParsedArgs();
      if (parsedArgs === null)
      {
         status.setCode(500, msg);
         return;
      }

      parsedArgs.pathNode = ParseArgs.resolveNode(parsedArgs.nodeRef);

      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 == "description")
         {
            description = field.value;
         }
         else if (field.name == "filedata" && field.isFile)
         {
            filename = field.filename;
            content = field.content;
         }
      }

      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 = parsedArgs.pathNode.createFile("upload" + parsedArgs.pathNode.children.length + "_" + filename) ;
         upload.specializeType("example:content");
         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.newNode = upload;
         exitUpload(200, "OK")
      }
   }
   catch (error){
      status.setCode(500, msg);
      return;
   }
}

main();

And the json freemarker for the response, no idea if it work, can't get the response
file : upload-content.post.json.ftl

{
    "item":
        {
            "type": "${newNode.type}",
            "name": "${newNode.name!''}",
            "fileName": "${newNode.name!''}",
            "path": "${newNode.displayPath}",
            "nodeRef": "${newNode.nodeRef}"
        }
}

Thank for your help

Outcomes