Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.

Create HTML file upload image/zip file in DAM

Avatar

Level 7

Hi Team,

 

I'm trying to create a DAM asset(image or zip file) which is uploaded through HTML file upload field but the asset is not getting created properly. I'm able to create uploaded image in file system(just to check whether input stream is correct or not) with the same input stream but not DAM asset. Please help.

HTML Multipart form

<form action="/bin/uploadDAMAsset" method="post" target="_blank" enctype="multipart/form-data">
<input type="file" id="inputFile" name="inputFile">
<input type="submit" value="Submit">
</form>

AEM Servlet code

Map<String, RequestParameter[]> requestParameterMap = request.getRequestParameterMap();
for(Entry<String, RequestParameter[]> paramPair : requestParameterMap.entrySet()) {
String key = paramPair.getKey();
if(key.equals("inputFile") ) {
RequestParameter[] values = paramPair.getValue();
RequestParameter paramVal = values[0];
try {
InputStream inputStream = paramVal.getInputStream();
ResourceResolver resourceResolver = request.getResourceResolver();
if(null != inputStream) {
AssetManager assetMgr = resourceResolver.adaptTo(AssetManager.class);
assetMgr.createAsset("/content/dam/test/emails/BB.jpg", inputStream, "image/jpeg", true);
}
} catch (IOException e) {
LOGGER.error("Error while getting the input stream and creating DAM asset: {} {}" , e.getMessage(), e);
}
}
}

 

Thanks in Advance!

AryA

@Lokesh_Shivalingaiah @kautuk_sahni 

11 Replies

Avatar

Community Advisor

Hi @Dinu_Arya 

 

Can you add the below code after assetMgr.createAsset() method:

resourceResolver.commit();

This will persist your changes.

You can see the adobe documentation here: https://www.adobe.io/experience-manager/reference-materials/6-5/javadoc/org/apache/sling/api/resourc...

 

 

Regards,

Arpit Varshney

Avatar

Level 7

Hi @ArpitVarshney ,

 

I tried this now but still no renditions and original rendition is empty.

 

Regards,

AryA

Avatar

Community Advisor

Hi @Dinu_Arya 

Is the loggedin user has appropriate permission to create dam assets. Can you try using admin account and check?

 

Also, please check error logs and share if you see any error?

 

Regards,
Arpit Varshney

Avatar

Level 7

Hi @ArpitVarshney ,

 

I'm trying this in author with admin account.

 

Regards,

AryA

Avatar

Community Advisor

Hi,

Please check https://github.com/arunpatidar02/aem63app-repo/blob/master/java/UploadAssets.java if this helps. This example shows how to upload the assets from local but you can get idea to create asset in DAM once you have InputStream



Arun Patidar

Avatar

Community Advisor

It was , in the url, corrected now.



Arun Patidar

Avatar

Community Advisor

Hi @Dinu_Arya 

When you are trying to create an asset, the resource resolver should be something that service user with appropriate permissions on that node. check the below link to create the user.

 

https://experienceleague.adobe.com/docs/experience-manager-64/administering/security/security-servic...

 

Hope this helps!

 

Thanks,

Kiran Vedantam.

Avatar

Level 7

Hi @Kiran_Vedantam ,

 

Yes. Actually I'm getting the resourceResolver from servlet's request. As suggested, I used system/service user flow of getting resource resolver but it also didn't help.

Avatar

Level 7

The issue is resolved by using byte array input stream.

Thank you for your inputs - @arunpatidar @ArpitVarshney @Kiran_Vedantam

 

Avatar

Level 3

Hi @Dinu_Arya @arunpatidar , 

I have used the implementation for image file upload but in backend 

Map<String, RequestParameter[]> requestParameterMap = request.getRequestParameterMap();

In requestParmeterMap i am not able to see key inputFile.

 

Any help