Skip to main content
Dinu_Arya
Level 6
Sep 25, 2021 10:22 AM UTC

Create HTML file upload image/zip file in DAM

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.

11 replies

ArpitVarshney
Community Advisor
Community Advisor
Sep 25, 2021 11:25 AM UTC

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

Translate
Dinu_Arya
Dinu_AryaAuthor
Level 6
Sep 25, 2021 6:14 PM UTC

Hi @arpitvarshney ,

 

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

 

Regards,

AryA

Translate
ArpitVarshney
Community Advisor
Community Advisor
Sep 26, 2021 7:13 AM UTC

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

Translate
arunpatidar
Community Advisor
Community Advisor
Sep 27, 2021 9:42 AM UTC

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

Translate
Arun Patidar
Dinu_Arya
Dinu_AryaAuthor
Level 6
Sep 27, 2021 3:53 PM UTC

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

Translate
arunpatidar
Community Advisor
Community Advisor
Sep 28, 2021 7:49 PM UTC

It was , in the url, corrected now.

Translate
Arun Patidar
Kiran_Vedantam
Community Advisor
Community Advisor
Sep 27, 2021 3:07 PM UTC

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.

Translate
Dinu_Arya
Dinu_AryaAuthor
Level 6
Sep 27, 2021 8:09 PM UTC

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.

Translate
Dinu_Arya
Dinu_AryaAuthor
Level 6
Sep 27, 2021 8:14 PM UTC

The issue is resolved by using byte array input stream.

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

 

Translate
Level 2
May 12, 2023 8:11 AM UTC

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

 

 

Translate