cancel
Showing results for 
Search instead for 
Did you mean: 

method write is not found ???

yascorp
Champ in-the-making
Champ in-the-making
Hello, I need some help to resolve my problem, I have a dashlet for sending files like images.
I need to transfer data to alfresco webscript so I use an ajax request, the problem is : I can't write my image content to the new createFile I have created, I tried to use :
newFile.properties.content.write(filecontent); but "method write is not found" ???

here is my files :

add-news.get.html.ftl
<javascript>
reader = new FileReader();
filter = /image.*/;
   
reader.onload = function(e) {
   document.getElementById('uploadPreview').src = e.target.result;
};

reader.onerror = function(e) {
    console.error("File can't be read ! Code " + e.target.error.code);
};

<#——————————————————————————————————->      
   
function loadImageFile() {
   if(document.getElementById('${args.htmlid}-images').files.length === 0) {return; }
   var files = document.getElementById('${args.htmlid}-images').files.item(0);
   if(!files.type.match(filter)) {
      alert("Choose another image !");
      return;
   }
   reader.readAsDataURL(files);
}


<#——————————————————————————————————->   

function generateForm() {

var title = new YAHOO.util.Element('${args.htmlid}-title').get('value');
var description = new YAHOO.util.Element('${args.htmlid}-description').get('value');
var category = new YAHOO.util.Element('${args.htmlid}-category').get('value');

var files = document.getElementById('${args.htmlid}-images').files;
   if(files) {
      for (var i = 0; i < files.length; i++){      
         files_v = files;   
         files_v['content'] = document.getElementById('uploadPreview').src;
      }
   }                  
   
   
var data = {
            "fields": [               
               {
                  title : title,
                  description : description,
                  category : category,
                  files: files_v
               }
             ]
         };


Alfresco.util.Ajax.request(
               {   
                   url: Alfresco.constants.PROXY_URI + "api/form/add-news",
                   method: Alfresco.util.Ajax.POST,
                  
                  dataObj:
                     {
                        site: Alfresco.constants.SITE,
                        data: data,                                                            
                     },
                     
                  requestContentType: Alfresco.util.Ajax.JSON,

                  
                  successCallback:                                          
                     {                     
                        fn: function generateForm_onSuccess(response) {
                           var dataString = JSON.stringify(data);                                                   
                           console.log("Submit success !" + dataString);                                                      
                        },
                        scope: this
                     },
                  failureCallback:
                     {
                        fn: function generateForm_onFailure(response) {
                           console.log("Loading failed …");
                        },
                        scope: this
                     },   
                  execScripts: true
               });
}
                            ………….
                            ………….
                            ………….

                                 <table id="product-table">
                         
                   <form id="${el}-form" name="form" method="POST" action="" enctype="application/json" accept-charset="utf-8" onsubmit="return validateForm();" >            
                      <tr>
                         <td><input type="checkbox" id="${el}-chk" name="chk" /></td>
                         <td>
                            <span>${msg("label.title")}
                               <input type="text" id="${el}-title" name="title" />
                            </span>
                         </td>
                         <td>
                            <span>${msg("label.description")}
                               <input type="text" id="${el}-description" name="description" />
                            </span>
                         </td>
                         <td>
                            <span>${msg("label.category")}
                               <select id="${el}-category" name="category" >
                                  <option value="Choose" selected></option>
                                  <option value="Food">Alimentaire</option>
                                  <option value="Finance">Finance</option>
                                  <option value="Clothes">Habillement</option>
                                  <option value="Media">Multimédia</option>
                                  <option value="News">News</option>
                               </select>
                            </span>
                         </td>
                         <td>
                            <#–<span class="yui-button yui-push-button" id="upload-button">
                               <span class="first-child">
                                  <button>${msg("button.upload")}</button>
                               </span>                        
                            </span>–>
                      
                            <input type="file" id="${el}-images" name="images" onchange="loadImageFile()" multiple ><br>
                            <img align=left id="uploadPreview" src="" width="50" height="50" border="none" alt="Image preview">                                                                                                                  
                         </td>
                      </tr>
                       
                             <span class="bottom-button">             
                               <input type="submit" id="${el}-ok-button" value="${msg('button.ok')}" onclick="generateForm()" />
                                 <input type="reset" id="${el}-cancel-button" value="${msg('button.reset')}" />
                              </span>
                              
                  </form>
               </table>
                             …………
                             …………
                             …………
</javascript>

Alfresco webscripts :

form-add-news.post.json.js
<javascript>
function main() {
      
   var siteId = json.get("site");
   
   var data = json.get("data");   
   
   var containerId = "documentlibrary";
   var siteRoot = siteService.getSite(siteId);
   var docLib = siteRoot.node.childByNamePath(containerId);
      
   var myObj = eval('(' + data + ')');   
              
   
   var title;
   var description; 
   var category;
   var file;
   var filename = null;
   var filecontent = null;
   var filetype = null;
   var filesize = null;
   
         
      if ( myObj.fields[0].hasOwnProperty("title") ) {
         title = myObj.fields[0].title;
      }
      if ( myObj.fields[0].hasOwnProperty("description") ) {
         description = myObj.fields[0].description;
      }
      if ( myObj.fields[0].hasOwnProperty("category") ) {
         category = myObj.fields[0].category;
      }
      if ( myObj.fields[0].hasOwnProperty("files") ) {            
         file = myObj.fields[0].files;
         filename = file.name;            
         filecontent = file.content.split(',')[1];
         filetype = file.type;
         filesize = file.size;            
      }


   var newDoc = docLib.createNode(title, "ca:entryFormNews");         
   newDoc.properties["ca:title"] = title;
   newDoc.properties["ca:description"] = description;
   newDoc.properties["ca:category"] = category;
   newDoc.save();
   
   
   var newFile = docLib.createFile("upload" + docLib.children.length + "_" + filename, "cm:content");   
   newFile.properties.content.mimetype = filetype;
   //newFile.properties.content.guessMimetype(filename);      
   //newFile.content = filecontent;
   newFile.properties.content.write(filecontent);
   newFile.properties.content.setEncoding("UTF-8");
      
   newFile.properties.title = title;
   newFile.properties.description = description;
   newFile.save();      
   model.newFile = newFile;      
   
         
   newDoc.createAssociation(newFile, "ca:file");
   
   model.newDoc = newDoc;
   
}

main();

</javascript>

my model :
customAlfresco.xml
<javascript>
<?xml version="1.0" encoding="UTF-8"?>
<!– Definition of new Model –>
<model name="ca:customAlfresco" xmlns="http://www.alfresco.org/model/dictionary/1.0">
   <!– Optional meta-data about the model –>
   <description>Alfresco's Customization</description>
   <version>1.0</version>
   <!– Imports are required to allow references to definitions in other models –>
   <imports>
      <!– Import Alfresco Dictionary Definitions –>
      <import uri="http://www.alfresco.org/model/dictionary/1.0" prefix="d" /><!– d = Dictionary Model –>
      <!– Import Alfresco Content Domain Model Definitions –>
      <import uri="http://www.alfresco.org/model/content/1.0" prefix="cm" /><!– cm = Content Model –>
   </imports>

   <!– Introduction of new namespaces defined by this model –>
   <namespaces>
      <namespace uri="org.obs.custom-alfresco.model" prefix="ca" /><!– ca = prefix of my custom model –>
   </namespaces>
   
   <constraints>
      <constraint name="ca:maxLengthTitle_constraint" type="LENGTH">
         <parameter name="maxLength">
            <value>50</value>
         </parameter>
      </constraint>
      <constraint name="ca:maxLengthDescription_constraint" type="LENGTH">
         <parameter name="maxLength">
            <value>256</value>
         </parameter>
      </constraint>
      <constraint name="ca:category_constraint" type="LIST">
         <parameter name="allowedValues">
            <list>
               <value>Food</value>
               <value>Finance</value>
               <value>Clothes</value>
               <value>Media</value>
               <value>News</value>               
            </list>
         </parameter>
      </constraint>
   </constraints>

   <types>
      <type name="ca:newsDoc">
         <title>Name</title>
         <description>Form Model</description>
         <parent>cm:content</parent>
      </type>
        
      <type name="ca:entryFormNews">
         <title>EntryNews</title>
         <parent>ca:newsDoc</parent>  
         <properties>
            <property name="ca:title">
               <title>Title</title>
               <type>d:text</type>
               <mandatory>true</mandatory>
               <constraints>
                  <constraint ref="ca:maxLengthTitle_constraint" />
               </constraints>
            </property>
            <property name="ca:description">
               <title>Description</title>
               <type>d:text</type>
               <mandatory>false</mandatory>
               <constraints>
                  <constraint ref="ca:maxLengthDescription_constraint" />
               </constraints>
            </property>
            <property name="ca:category">
               <title>Category</title>
               <type>d:text</type>
               <constraints>
                  <constraint ref="ca:category_constraint" />
               </constraints>
            </property>
         </properties>
         <associations>
            <association name="ca:file">
                 <source>
                                         <mandatory>false</mandatory>
                                         <many>false</many>
                                     </source>
                 <target>
                     <class>cm:content</class>
                <mandatory>true</mandatory>
                <many>true</many>
                 </target>
            </association>
         </associations>
      </type>
   </types>


</model>
</javascript>



3 REPLIES 3

jpotts
World-Class Innovator
World-Class Innovator
If you comment out the write statement does your document get created successfully with the values you expect?

Jeff

yascorp
Champ in-the-making
Champ in-the-making
Thank you Jeff for your reply !
Yes the document is created but the uploaded image won't be displayed and It's seems damaged.
I think I have to use multipart content type to transfer my data with submit button, but It's hard to do it from a dashlet to an alfresco webscript.
How can I define my action form if my descriptor webscript file url is : /api/form/add-news ???
Thanks & regards 

jpotts
World-Class Innovator
World-Class Innovator
Your form action URL can leverage the Share proxy URL. That way, your form can post to Share and Share will forward the post on to the repository tier. If you do a recursive grep on $TOMCAT_HOME/webapps/share/WEB-INF/classes/alfresco/site-webscripts you'll find lots of examples of that.

Jeff