how to pass file from jquery to servlet in aem?

Avatar

Avatar

tahir1601

Avatar

tahir1601

tahir1601

27-06-2019

Hi,

Can anyone help me out in this usecase where attachments are sent as email in aem. I am stuck at a point on how to pass file from jquery ajax to servlet and then passing it as parameter to emailService.

Joerg HohArun Patidarsmacdonald2008

Accepted Solutions (1)

Accepted Solutions (1)

Avatar

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831

Avatar

Arun_Patidar

MVP

Total Posts

2.9K

Likes

1.0K

Correct Answer

831
Arun_Patidar
MVP

27-06-2019

In jquery you can pass the file path and read file in Servlet and convert in InputStream and use inside your map

File file = new File(filePath);

InputStream is = new FileInputStream(file);

ByteArrayDataSource(InputStream is, String type)

Email

Email API

String attachment1 = "This text should be in the attache txt file.";
  Map<String, DataSource> attachments = new HashMap<>();
  attachments.put("attachment1.txt", new ByteArrayDataSource(attachment1, "text/plain"));

Answers (2)

Answers (2)

Avatar

Avatar

joeb21688728

Avatar

joeb21688728

joeb21688728

27-06-2019

Here is an older doc that covers HOW TO pass a file to an AEM servlet using JQuery:

Adobe Experience Manager Help | Uploading files to Adobe Experience Manager DAM using AssetManager A...

Avatar

Avatar

antoniom5495929

Avatar

antoniom5495929

antoniom5495929

27-06-2019

Hi,

what you can try to do is to encode your file in base64 and then send it as a string parameter into your servlet.

Inside your servlet you just need to use some snippet like the following:

Base64.getDecoder().decode(fileBase64)

In order to get the file and decode it into your serlvet.

Let me know if this could be helpful.

Thanks,

Antonio