Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
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

5 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 ?

Avatar

Level 2

Hi @santhosh_kumark,

I have almost the similar kind of question - https://experienceleaguecommunities.adobe.com/t5/adobe-experience-manager/upload-asset-to-dam-path-u...

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!

Avatar

Community Advisor

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.