Tuesday, 15 July 2014

Upload in CQ5

Example:
Type 1)
<form action="http://localhost:4502/content/dam/einstants/*" method="POST" enctype="multipart/form-data"> 
<input name="./jcr:content/renditions/original" type="file"> 
<input name="./jcr:primaryType" value="dam:Asset" type="hidden"> 
<input name="./jcr:content/jcr:primaryType" value="dam:AssetContent" type="hidden"> 
<input name="./jcr:content/renditions/original@TypeHint" value="nt:file" type="hidden"> 
<input name="./jcr:content/metadata/jcr:primaryType" value="nt:unstructured" type="hidden"> 
<input name="./jcr:content/metadata/dc:title" value="Title of the asset" type="hidden"> 
<input type="submit"> 
</form>


Type 2)


 CQ already has Apache Commons FileUploadinstalled, so you decide to use it to handle the file upload.
You write your servlet code and end up with something like this:
// Check that we have a file upload request
final boolean isMultipart = ServletFileUpload.isMultipartContent(request);
PrintWriter out = null;
try {
  out = response.getWriter();
  if (isMultipart) {
    final Map<String, RequestParameter[]> params = request.getRequestParameterMap();
    for (final Map.Entry<String, RequestParameter[]> pairs : params.entrySet()) {
      final String k = pairs.getKey();
      final RequestParameter[] pArr = pairs.getValue();
      final RequestParameter param = pArr[0];
      final InputStream stream = param.getInputStream();
      if (param.isFormField()) {
        out.println("Form field " + k + " with value " + Streams.asString(stream) + " detected.");
      } else {
        out.println("File field " + k + " with file name " + param.getFileName() + " detected.");
      }
    }
  }


Type 3)

<%@page import="javax.jcr.Session"%>
<%@page import="javax.jcr.Node"%>
<%@page import="java.io.ByteArrayInputStream"%>
<%@page import="java.util.Calendar"%>
<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0"%>
<sling:defineObjects/>
<%// get the root nodeSession 
mySession = slingRequest.getResourceResolver().adaptTo(Session.class);
Node root = mySession.getRootNode();
// create a new node of type nt:fileNode
myNewNode = root.getNode("content").addNode("mynewnode", "nt:file");

Node contentNode = myNewNode.addNode("_jcr_content", "nt:resource");
// set the mandatory properties

contentNode.setProperty("jcr:data", new ByteArrayInputStream(request.getParameter("myfile").getBytes()));
contentNode.setProperty("jcr:lastModified", Calendar.getInstance());
contentNode.setProperty("jcr:mimeType", "mymimetype");

mySession.save();%>

3 comments:

  1. param.getInputStream(); return null value after i uploaded the file and click on submit, can any one help on how to get the uploaded file stream in servlet after click on submit

    ReplyDelete
    Replies
    1. Hi Selvaraj,

      Are you able to debug it? isMultipart coming as true?
      Can you confirm that you are passing form attribute as enctype="multipart/form-data"

      Delete
    2. Also try like this
      request.getParameter("myfile").getBytes()

      Delete