diff options
author | Sergey Ponomarev <stokito@gmail.com> | 2023-09-21 22:48:54 +0300 |
---|---|---|
committer | Sergey Ponomarev <stokito@gmail.com> | 2024-04-09 10:43:35 +0300 |
commit | 4b771b3df0c39baed5e0904ad66e460691194636 (patch) | |
tree | f45655fda2e70f19e37fc4d0c33b056360a9765e /modules/luci-base/htdocs/luci-static/resources/ui.js | |
parent | 5d2d52802229608d65ec973a1984194b8baa60fa (diff) |
luci-base: ui.js FileUpload: option to enable Download button
Allow downloading from a file browser.
The Download button is located near to Delete.
It's shown only for files: folders or /dev/ devices can't be downloaded.
The downloading is made via fs.read_direct() which internally calls cgi-download.
Signed-off-by: Sergey Ponomarev <stokito@gmail.com>
Diffstat (limited to 'modules/luci-base/htdocs/luci-static/resources/ui.js')
-rw-r--r-- | modules/luci-base/htdocs/luci-static/resources/ui.js | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/modules/luci-base/htdocs/luci-static/resources/ui.js b/modules/luci-base/htdocs/luci-static/resources/ui.js index 1b14c60c64..8d41962ac9 100644 --- a/modules/luci-base/htdocs/luci-static/resources/ui.js +++ b/modules/luci-base/htdocs/luci-static/resources/ui.js @@ -2636,6 +2636,9 @@ var UIFileUpload = UIElement.extend(/** @lends LuCI.ui.FileUpload.prototype */ { * remotely depends on the ACL setup for the current session. This option * merely controls whether the file remove controls are rendered or not. * + * @property {boolean} [enable_download=false] + * Specifies whether the widget allows the user to download files. + * * @property {string} [root_directory=/etc/luci-uploads] * Specifies the remote directory the upload and file browsing actions take * place in. Browsing to directories outside the root directory is @@ -2650,6 +2653,7 @@ var UIFileUpload = UIElement.extend(/** @lends LuCI.ui.FileUpload.prototype */ { show_hidden: false, enable_upload: true, enable_remove: true, + enable_download: false, root_directory: '/etc/luci-uploads' }, options); }, @@ -2931,6 +2935,10 @@ var UIFileUpload = UIElement.extend(/** @lends LuCI.ui.FileUpload.prototype */ { 'class': 'btn', 'click': UI.prototype.createHandlerFn(this, 'handleReset') }, [ _('Deselect') ]) : '', + this.options.enable_download && list[i].type == 'file' ? E('button', { + 'class': 'btn', + 'click': UI.prototype.createHandlerFn(this, 'handleDownload', entrypath, list[i]) + }, [ _('Download') ]) : '', this.options.enable_remove ? E('button', { 'class': 'btn cbi-button-negative', 'click': UI.prototype.createHandlerFn(this, 'handleDelete', entrypath, list[i]) @@ -2995,6 +3003,22 @@ var UIFileUpload = UIElement.extend(/** @lends LuCI.ui.FileUpload.prototype */ { }, /** @private */ + handleDownload: function(path, fileStat, ev) { + fs.read_direct(path, 'blob').then(function (blob) { + var url = window.URL.createObjectURL(blob); + var a = document.createElement('a'); + a.style.display = 'none'; + a.href = url; + a.download = fileStat.name; + document.body.appendChild(a); + a.click(); + window.URL.revokeObjectURL(url); + }).catch(function(err) { + alert(_('Download failed: %s').format(err.message)); + }); + }, + + /** @private */ handleSelect: function(path, fileStat, ev) { var browser = dom.parent(ev.target, '.cbi-filebrowser'), ul = browser.querySelector('ul'); |