AEM Form Upload File | Community
Skip to main content
December 16, 2021
Solved

AEM Form Upload File

  • December 16, 2021
  • 2 replies
  • 2025 views

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.

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Best answer by santhosh_kumark

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

2 replies

santhosh_kumark
santhosh_kumarkAccepted solution
Level 6
December 16, 2021

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

user06594Author
December 17, 2021

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 ?

santhosh_kumark
Level 6
January 11, 2022

yes

Level 2
January 9, 2023

Hi @santhosh_kumark,

I have almost the similar kind of question - https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/upload-asset-to-dam-path-using-coral-fileupload/m-p/566262#M141308

Want to understand your answer : so, how would the asset be created in DAM path?

Can we use the Asset HTTP API in such cases?

 

Thanks in advance!

santhosh_kumark
Level 6
February 13, 2023

Yes we can use Asset API. we need to append the binary payload/response in request body and it will upload in specified dam location.