Durchsuchbare Dokumentation aufrufen | Zurück zur Dokumentationsübersicht
Navigation: Dokumentationen agorum core > agorum core aguila
Dieses Widget stellt eine Schaltfläche dar, die das Hochladen von Dateien ermöglicht.
Der Upload kann entweder durch einen Dateiauswahl-Dialog (dargestellt, wenn ein Benutzer die Schaltfläche anklickt) oder durch Ziehen von Dateien mit der Maus gestartet werden. Benutzer können hierbei auch mehrere Dateien auf einmal selektieren und hochladen.
let aguila = require('common/aguila'); let widget = aguila.create({ type: 'agorum.upload', text: 'Datei wählen ...', width: 200 }); widget.on('uploadQueueChanged', uploadQueue => { // ... }); widget.on('uploading', is => { // ... });
Alle Events in agorum core aguila - Widget gelten.
Definiert den Text auf der Schaltfläche.
keine
let aguila = require('common/aguila'); let objects = require('common/objects'); let templates = require('common/templates'); let transaction = require('common/transaction'); let widget = aguila.create({ type: 'agorum.vbox', width: 400, height: 500, upload: true, items: [ { name: 'grid', type: 'agorum.basicGrid', border: true, flexible: true, columns: [ { name: 'name', text: 'Name', flexible: true }, { name: 'progress', text: 'Progress', width: 100 } ] }, { name: 'button', type: 'agorum.upload', text: 'Choose file' } ] }); let grid = widget.down('grid'); let button = widget.down('button'); let active = []; let progress = {}; let percentage = progress => (progress * 100).toFixed(0) + '%'; let update = () => { // collect state for all files let files = widget.uploadQueue.concat(button.uploadQueue); // update grid, calculate total progress let count = files.length; let total = 0; let done = 0; grid.rows = files.map(file => { file.done = progress[file.id] || 0; total += file.size; done += file.done; return { name: file.name, progress: percentage(file.done / file.size) }; }); // update button text if (count > 0) { button.text = 'Uploading ' + count.toFixed(0) + ' files - ' + percentage(done / total); } else if (active > 0) { button.text = 'Finalizing ' + active.toFixed(0) + ' files...'; } else { button.text = 'Choose file'; } }; let updateProgress = (id, add) => aguila.enter(() => { progress[id] = (progress[id] || 0) + add; update(); }); let upload = is => { aguila .fork(() => transaction(t => { // upload into new file let file = objects.create('file', { name: is.name, target: objects.find('home:MyFiles').createPath('temp') }); // mark as temporary file.systemFlags = 16384; let raf = new Packages.agorum.roi.ejb.common.DbRandomAccessFile(file.sessionController, file); try { raf.doNotMakeHistory = true; let buffer = java.lang.reflect.Array.newInstance(java.lang.Byte.TYPE, file.blockSize * 10); let commit = Date.now() + 10000; let read; while ((read = is.read(buffer)) > 0) { // write chunk raf.write(buffer, 0, read); // update progress for this file updateProgress(is.id, read); // commit every 10s if (Date.now() > commit) { t.restart(); commit = Date.now() + 10000; } } } finally { raf.close(); } // remove temporary mark file.systemFlags = 0; })) .catch(err => console.log('Upload failed', is.name, err)) .finally(() => { // ensure stream is closed (already done if objects.create was successful) is.close(); // remove from active uploads list active = active.filter(other => other !== is); update(); }); // add to active uploads list active.push(is); }; // listen for queue updates button.on('uploadQueueChanged', update); widget.on('uploadQueueChanged', update); // listen for new uploads button.on('uploading', upload); widget.on('uploading', upload); // stop uploads if widget is destroyed widget.on('destroyed', () => active.forEach(is => is.close())); widget;