Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to automatically download attachment ?

Albert Cameron August 8, 2021

I am creating an attachment on the server.

Now the bonus would be to automatically download it on the user's local storage (e.g. Downloads folder) when a certain transition is triggered. 

Is there any way to do this. The browser only knows HTML and JavaScript so it only could be possible to use some Web technology. 

1 answer

0 votes
Kishan Sharma
Community Champion
August 8, 2021

Hi @Albert Cameron You can write a postfunction on the workflow transition to download attachment from JIRA via REST API. Refer this link for the method.

Albert Cameron August 8, 2021

Ok. I understand. So by using this REST call I will get the URL of the attachment. But then  I would have to use javaScript or HTML to download automatically. So where do I have to put this JavaScript code ? 

Kishan Sharma
Community Champion
August 9, 2021

Using REST API you can get the URL of the attachment, as well as download it. You can script it in groovy and add new post function to your workflow transition. Please find the instructions for adding a post function on this link.

Albert Cameron August 9, 2021

Ok. From the postfunction I can call first the REST API to get the metadata . So the URL to get the actual File would be e.g. "self": "http://www.example.com/jira/rest/api/2.0/attachments/10000",

Now comes the difficult part. If I just call this URL for the actual file  from the postfunction it doesn't help me. I need a way to return the file to the client. 

So maybe I will need a Web Resource which I call to return the file. 

You shouldn't forget that all this postfunction stuff happens on the server. But the file has to be returned to the client.

The only way to get the file on the client is to use JavaScript. I am pretty sure about that. But where to put the JavaScript . That is my question right now. 

Albert Cameron August 9, 2021

My collegue just came up with a solution. He just uses a plain button. And this button calls a Web Resource. For our scenario this is fine. But I really would not know how to do this with a postfunction.

Like Kishan Sharma likes this
Kishan Sharma
Community Champion
August 9, 2021

At first, you can add an attachment to a test issue. Then using Get Issue method, get its metadata -
eg: https://example.com/rest/agile/1.0/issue/{issueID}

The response will contain an attachment’s content field (eg: "content":"https://example.com/jira/secure/attachments/10000/picture.jpg") which is URL for this attachment.

Now you can make a GET https://example.com/jira/secure/attachments/10000/picture.jpg request to download this attachment.

Kishan Sharma
Community Champion
August 9, 2021

Please share what solution you have found out, it might be useful to others.

Albert Cameron August 10, 2021

My collegue now just uses a simple button (implemented as a Scriptrunner Web Fragment) to send a request to a Jira REST module. This way when the api sends back the file it is automatically downloaded. Good idea.

But it has one disadvantage. If the REST api throws a WorkflowException it will not be shown to the jira user. So all the validation on the server is useless for the jira user. In a REST Api you normally send back an error Object. But as we now use a simple button we can not process this error object. Really bad in terms of showing the jira user appropriate error messages.

Like Kishan Sharma likes this
Kishan Sharma
Community Champion
October 27, 2021

Thank you for sharing @Albert Cameron You can create a new answer to this question and mark it as accepted.

Suggest an answer

Log in or Sign up to answer