Expand my Community achievements bar.

Learn about Edge Delivery Services in upcoming GEM session

How to get path of uploaded file in servlet from js

Avatar

Level 2

Hi there

I am trying to upload a file using type = "file" in aem front end and I want to read that file using servlet and get the data of the file. I already have a servlet in which I have hardcoded one link but I want to get the link of the file uploaded using the button in that place and get data of the file.
Can anyone help me with this?
Thanks in advance!!

8 Replies

Avatar

Employee Advisor

@sushmapoojitha 

When you add attachments to an adaptive form, the attachments are stored in a temporary location in the CRX repository. 

You can also create a servlet to store the form attachments in a new location in the CRX repository and then access the file directly.

String fileName = attacmenPath.split("/")[1];
InputStream is = request.getPart(attacmenPath).getInputStream();
Document aemFDDocument = new Document(is);
String crxPath = saveDocumentInCrx("/content/afattachments", fileName, aemFDDocument);

 

Hope this helps!

Avatar

Level 2

Hi @Pulkit_Jain_, I don't want to save the uploaded attachment, it's just after uploading file using one button and on click on submit button, my servlet which gets active by the submit button should take the absolute path of the uploaded file and should be able to read the data in that file.
or can you help me get the absolute path of the uploaded file in aem js itself?

Avatar

Employee Advisor
@sushmapoojitha Please confirm if you're using AEM Forms components in your use-case? If so, you will have the direct access to the file(s) uploaded to the form ( default location- /content/forms/fp/admin/submit/attachments/T6J7FEH4V3LNK6T6USYBD7ZFQA). This file path can be accessed/read for the data in the file once the MIME type is set in your servlet. With the example i shared above, you should be able to set a custom path to the file attachment as well.

Avatar

Level 2
Hi @Pulkit_Jain_, yes I am using AEM Forms component only, but the thing here is i don't want to use "content" to get the file to my servlet. Is there any other way?

Avatar

Employee Advisor
@sushmapoojitha There are multiple ways to do that: 1. rewrite module in your web server or 2. Making use of URL Mappings (a.k.a resource.resolver.mapping) - https://experienceleague.adobe.com/docs/experience-manager-64/deploying/configuring/resource-mapping... or 3. Simply save the attachment to a different path if NOT "/content" https://experienceleague.adobe.com/docs/experience-manager-learn/forms/store-and-retrieve-af-with-2f...

Avatar

Employee Advisor

@sushmapoojitha " absolute path of the uploaded file", So you want to expose part of your server with an absolute path that is accessible to an external user, Not a great idea.

 

How many links do you want to show on the form?

 

Avatar

Level 2
@Mayank_Gandhi, it is just one link which i need, and this cannot be used by all users, it will be used only by a certain set of people and also I don't need to store the uploaded file, i just need to get link of it send to servlet and get data inside the file. It is for one time purpose, no need of storing file inside aem.

Avatar

Employee Advisor

@sushmapoojitha Why not just create a custom file upload widget and as users upload the file convert it to base64 and create an HTML link in the form. You don't need a servlet for this, it can be done in vanilla JS.