Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

AEM Form Upload File

Avatar

Level 1

Hi everyone, im new to AEM, i want to know that it is possible to create component filled with form input including file upload, and the uploaded file is gonna stored in dam (like if we manually upload asset using built-in asset file uploader menu in aem), is there any documentation or article that i can read ? thanks for your help.

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @user06594 ,

 

Try to get the required file reference from component dialog and in code get the inputstream of the file and convert it to binary. The binary content need to be set as "jcr:data" property of asset node path wherever you wanted to store it. PFB sample code.

 

File file = new File("/path/to/document.pdf");
FileInputStream is = new FileInputStream(file);
mimeType = "application/octet-stream";
Node node = session.getNode(assetRootNode);
ValueFactory valueFactory = session.getValueFactory();
Binary contentValue = valueFactory.createBinary(is);
Node fileNode = node.addNode(fileName, "nt:file");
fileNode.addMixin("mix:referenceable");
Node resNode = fileNode.addNode("jcr:content", "nt:resource");
resNode.setProperty("jcr:mimeType", mimeType);
resNode.setProperty("jcr:data", contentValue);
Calendar lastModified = Calendar.getInstance();
lastModified.setTimeInMillis(lastModified.getTimeInMillis());
resNode.setProperty("jcr:lastModified", lastModified);
session.save();

 

 

Regards,

Santosh

View solution in original post

3 Replies

Avatar

Correct answer by
Community Advisor

Hi @user06594 ,

 

Try to get the required file reference from component dialog and in code get the inputstream of the file and convert it to binary. The binary content need to be set as "jcr:data" property of asset node path wherever you wanted to store it. PFB sample code.

 

File file = new File("/path/to/document.pdf");
FileInputStream is = new FileInputStream(file);
mimeType = "application/octet-stream";
Node node = session.getNode(assetRootNode);
ValueFactory valueFactory = session.getValueFactory();
Binary contentValue = valueFactory.createBinary(is);
Node fileNode = node.addNode(fileName, "nt:file");
fileNode.addMixin("mix:referenceable");
Node resNode = fileNode.addNode("jcr:content", "nt:resource");
resNode.setProperty("jcr:mimeType", mimeType);
resNode.setProperty("jcr:data", contentValue);
Calendar lastModified = Calendar.getInstance();
lastModified.setTimeInMillis(lastModified.getTimeInMillis());
resNode.setProperty("jcr:lastModified", lastModified);
session.save();

 

 

Regards,

Santosh

Avatar

Level 1

thanks for answering, CMIIW, so i need to get the file and then convert base64 and then convert it to binary, and send the binary reference to js-use ?