Create HTML file upload image/zip file in DAM | Community
Skip to main content
Dinu_Arya
Level 6
September 25, 2021

Create HTML file upload image/zip file in DAM

  • September 25, 2021
  • 4 replies
  • 4055 views

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 

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

4 replies

ArpitVarshney
Community Advisor
Community Advisor
September 25, 2021

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/resource/ResourceResolver.html#commit--

 

 

Regards,

Arpit Varshney

Dinu_Arya
Dinu_AryaAuthor
Level 6
September 25, 2021

Hi @arpitvarshney ,

 

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

 

Regards,

AryA

ArpitVarshney
Community Advisor
Community Advisor
September 26, 2021

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

arunpatidar
Community Advisor
Community Advisor
September 27, 2021

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
Dinu_Arya
Dinu_AryaAuthor
Level 6
September 27, 2021

I'm getting 404. Looks like blob folder is not there.

arunpatidar
Community Advisor
Community Advisor
September 28, 2021

It was , in the url, corrected now.

Arun Patidar
Kiran_Vedantam
Community Advisor
Community Advisor
September 27, 2021

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-service-users.html?lang=en

 

Hope this helps!

 

Thanks,

Kiran Vedantam.

Dinu_Arya
Dinu_AryaAuthor
Level 6
September 27, 2021

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.

Dinu_Arya
Dinu_AryaAuthor
Level 6
September 27, 2021

The issue is resolved by using byte array input stream.

Thank you for your inputs - @arunpatidar @arpitvarshney @kiran_vedantam

 

May 12, 2023

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