Get Value from "Attachment" field

Binisha March 27, 2013

Hi All,

I have added the field "Attachment" to the transition screen and i am attaching file. I wanted to get the value from this field.

Please provide a code snippet to get the information.

Thanks

2 answers

0 votes
Onkar Ahire
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
March 27, 2013
List<Attachment> attachments = this.attachmentManager.
getAttachments(issue);
for (Attachment attachment : attachments) 
{
    System.out.println(attachements.getFilename());
    // Apply the logic to retrieve the content of the file 
}

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 27, 2013

The field does not have a value.

You need to work with a file that Jira needs to retrieve for you which is very different from a field value. Could you explain exactly what you want to do with the attachments here?

For eample, do you need the information about where the user is uploading from? The content of the file? The file name the user gives? The file name Jira will use on disk? Something else?

Binisha March 27, 2013

Hi Nic,

Thanks for the response. I need to get the file name of the attached file and then get its information like md5checksum etc.

Regards

Binisha

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 27, 2013

Ok,

You need to create or obtain an attachmentManager object in your code, then you can use the function getAttachments(Issue) to get the list of attachments. You can then get some more information from the attachment objects - getAuthor, getFilename and so-on. There's no md5 function built-in, you'll need to code that for yourself

Binisha March 27, 2013

Hi Nic,

The main goal is to get the name of the file that we are attaching and add the filename to a field using postfunction.

And also before attaching we need to compare whether this file is existing, by generating the checksum of the file we are attaching and comparing with the files, which are already attached. If the file exists then we need to throw an error to the user. - Validator

Is this feasible?

Thanks

Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 27, 2013

Ah, that's not quite what you said - I was unclear on whether you meant "the file to be attached by the user" and "the file already attached to the issue". You actually need both! You've also got two separate processes to do

So, you do the validation first. Your code will need to

  1. Look at the mutable issue passed to the validator - this contains a handle on the issue you're working on, along with what the user has entered.
  2. For the new attachment, calculate it's md5 checksum
  3. Read the list of current attachments (Onkar's code is what you need here) and md5 them
  4. If the new attachment md5 matches one found on the list, return a "fail" to the user

Secondly, you want the file name in a field. As you say, that's a post-function, to run after validation, but is easier. All you need is Onkar's code again, with a line that gets each file name and puts it into your field.

Suggest an answer

Log in or Sign up to answer