Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.
SOLVED

How to get full path when upload file choose desktop file workflow?

Avatar

Level 3

Hi Team,

I done customization for startworkflow.jsp("/libs/cq/gui/components/authoring/workflow/startworkflow/startworkflow.jsp").

When click upload file choose option getting fakepath append filename,How to get full path directory need to send email attachment.

fakepath.PNG

fakepath1.PNG

I am not able  send file attached by using fakepath. error message : java.io.FileNotFoundException: C:\fakepath\testJcrcontent.txt(The system cannot find the path specified).

EmailAttachment attachment = new EmailAttachment();

  attachment.setPath("C:\fakepath\testJcrcontent.txt");

        attachment.setDisposition(EmailAttachment.ATTACHMENT);

        attachment.setDescription("Any Description");

        attachment.setName("Any name you can set");

Thanks

Kotireddy

1 Accepted Solution

Avatar

Correct answer by
Level 10

Pick the file using File API like   File file = new File(path_to_file); and then use file.getPath();

Alternatively, if you want to use a generic solution rather than a system dependent folder structure, try this -

File directory = new File("."); -- current WD for AEM, map the file path relative to AEM directory somewhere outside crx-quickstart

String path = directory.getCanonicalPath() + File.separator + LOCAL_FOLDER + File.separator;  -- File.separator would work both on Win & Linux

File file = new File(path + filename);

attachment.setPath(file.getPath());

View solution in original post

6 Replies

Avatar

Correct answer by
Level 10

Pick the file using File API like   File file = new File(path_to_file); and then use file.getPath();

Alternatively, if you want to use a generic solution rather than a system dependent folder structure, try this -

File directory = new File("."); -- current WD for AEM, map the file path relative to AEM directory somewhere outside crx-quickstart

String path = directory.getCanonicalPath() + File.separator + LOCAL_FOLDER + File.separator;  -- File.separator would work both on Win & Linux

File file = new File(path + filename);

attachment.setPath(file.getPath());

Avatar

Level 2
Hi @Gaurav-Behl, can you suggest how to pick file using File API. Can you provide any example for that through which we can get absolute path of the file uploaded. Thank in advance!!

Avatar

Level 2

Hi @Gaurav-Behl, actually I am uploading a file using js script

" $("#fileupload").click(function(){
var input = document.createElement("input");
input.type = "file";
input.id="myfile";
input.name="myfile";
input.onchange = _ => {
// you can use this method to get file and perform respective operations
let uploadedFile = input.files;
//document.getElementById('target_div').appendChild(input);
console.log(uploadedFile[0]);
console.log(uploadedFile[0].name);
};
input.click();
});"

on button click. I need to send the absolute path of this file to my servlet which on getting path will read the file and submit data. But here i am not able to get absolute path for the uploaded file. Is there any way to get the absolute path as I read in many places that due to security reasons some browsers don't support it. Is there any way for this? Thanks in advance!

Avatar

Level 10
This is correct, browsers are restricted to local filesystem access by default otherwise anybody could run malicious scripts. A better way is to ask for some form of "user-input" so that user has to key-in the required info. Based on your requirements, you may have to explore alternatives. P.S. Absolute file path will vary based on underlying OS and file/folder sharing permissions as well.