Download button for all attachments of the document and its children
| This snipped can be used in a macro or on a page to provide a button for the users to automatically download the attachments of the current Documents as well as its child documents. |
| Type | Snippet |
| Category | Other |
| Developed by | |
| Rating | |
| License | Unknown |
Table of contents
Description
The download is realized via JavaScript that is provided by a JavaScriptExtension object containing the following code:
require(['jquery'], function($) { $(document).on('xwiki:dom:updated', function(e, data) { $('#$request.buttonID').off('click').on("click",runDownload) }) }); async function runDownload(e){ e.preventDefault(); const response = await fetch(`https://${window.location.hostname}/rest/wikis/${XWiki.currentWiki}/attachments?media=json&space=${XWiki.currentSpace}&number=200`); const attachments = await response.json(); for(const attachment of attachments.attachments){ window.open(attachment.xwikiAbsoluteUrl, '_blank') } }The runDownload function fetches a list of attachments from the API endpoint and then opens them each directly in new browser tabs to start the downloads.
This can result in quite a number of tabs being opened which is why the user typically has to allow pop ups for XWiki. It unfortunately only initiates an automatic download when the filetype cannot be displayed by the browser. This means that a mass download of images for example is not very convenient.
The query can be attachment further customized to restrict the media type. E.g. to only download table documents attach the following paramenter to the query string: "&types=application%2Fvnd.openxmlformats-officedocument.spreadsheetml.sheet,application%2Fvnd.ms-excel".
Note that the "number=200" parameter sets the amount of attachments returned by the server. Change this according to your use case. In my experience even downloading 100 attachments in separate tabs after clicking the button works with surprising performance.
The "require"-part of the code is for attaching the runDownload function to a button on the page - see below.
To provide a button i.e. macro with the functionality add a macro object with the following code:
{{velocity}} #set ($buttonID = "$mathtool.random(1,100000)-downloadButton") #set ($script = $xwiki.jsx.use('Path.to.JSExtensionOjectPage.WebHome', {'buttonID': $buttonID })) {{html clean="false"}} <div class="tableDownloadButtonMacro"> #if ($xcontext.action == 'view') <div> <input type="hidden" name="form_token" value="$!{services.csrf.getToken()}"/> <fieldset> <label class="btn btn-default" for="${buttonID}"> Download all attachments from this documents and its children. </label> <Button id=${buttonID} style="display: none" type="file"> </fieldset> </div> #end </div> {{/html}} {{/velocity}}Feel free to further improve this snippet. This might not be the optimal or most secure way to realize such a functionality.