1 Reply Latest reply on Mar 30, 2012 12:21 AM by kasq

    How to upload a file with form?


      Hi all,


      I created a jsp with a form. I would like to include upload funtion in that form. Is there a CQ upload component used to upload file?


      Sample code will be good.


      Any help will be great! Thanks.

        • 1. Re: How to upload a file with form?
          kasq Level 3

          Hi Andy,


          If you are using the form with some input filed with type="file" first of all the form should have set enctype="multipart/form-data".


          Second thing is to have in the component which generate file input like this:


          <input id="fileUploadInput" name="./*" type="file">


          where name="./*" means that the uploaded file name will be used to create a node name the same like file name.


          The last thing depends what do you want to achive. In our implementation we are using custom sling post servlet to get data send by the user from the form and store it in some path in the repository. This is a simple code to check if the file is in the request params map and then to create a file in the repository:


          RequestParameterMap paramsMap = request.getRequestParameterMap()

          Set<Map.Entry<String,RequestParameter[]>> set = paramsMap.entrySet();

          Iterator<Map.Entry<String,RequestParameter[]>> iter = set.iterator();

          while (iter.hasNext()) {

               Map.Entry<String,RequestParameter[]> prop = (Map.Entry<String,RequestParameter[]>)iter.next();

               RequestParameter[] values = prop.getValue();

               for (RequestParameter requestParameter : values) {

                      RequestParameter value = requestParameter;

                      if (value.getSize() <= 0) {


                      } else  (value.getContentType()!=null) {






          createFile function:


          private void createFile(RequestParameter value) throws RepositoryException{

                 String fileName =  value.getFileName();

                 fileName = fileName.substring(fileName.lastIndexOf('/') + 1);

                 fileName = fileName.substring(fileName.lastIndexOf('\\') + 1);

                 fileName = Text.escapeIllegalJcrChars(fileName);




          createFileNode function (shuqNode is a node in the repository bellow which the file should be added):


          private void createFileNode (String fileNodeName,RequestParameter value) throws RepositoryException {


                 List<Modification> changes = new ArrayList<Modification>();

                 boolean flag = false;


                //this part of code check if bellow shuqNode the file exists, if yes it is removed

                 if (shuqNode.hasNodes()) {

                     if (!shuqNode.hasNode(fileNodeName))






                 if (!flag) {

                     Node resParent= shuqNode.addNode(fileNodeName,"nt:file");



                     Node res = resParent.addNode("jcr:content","nt:resource");



                     String fileContentType =  value.getContentType();

                     if (fileContentType != null) {

                         int idx = fileContentType.indexOf(';');

                         if (idx > 0) {

                             fileContentType = fileContentType.substring(0, idx);





                         res.setProperty("jcr:lastModified", Calendar.getInstance()).getPath()



                         res.setProperty("jcr:mimeType", fileContentType).getPath()



                     try {


                             res.setProperty("jcr:data",createValue(value.getInputStream(),shuqNode.getSession())).get Path()


                     } catch (IOException e) {

                           log.warn("Error while adding jcr:data for new file node node", e);





          That's all . I hope that this code helps you.