cancel
Showing results for 
Search instead for 
Did you mean: 

Catch formdata response in simpledialog

jdupont
Champ in-the-making
Champ in-the-making
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
2 REPLIES 2

afaust
Legendary Innovator
Legendary Innovator
Hello,

in this case, you should try and debug simple-dialog.js and the onSuccess function in that file using a tool like FireBug. The way you have defined your callback appears to be fine, so unless you have any errors in a console somewhere (browser console or server), there is little help I could think of.

Regards
Axel

simas
Champ in-the-making
Champ in-the-making
Hi,

Did you ever get it to work as you would expect? I have the same problem and getting the "onSuccess" function to execute is crucial for what I'm doing.