Caricare file con UPLOAD() e avanzamento
UPLOAD('POST /url/', formdata, callback, onprogress) invia un FormData (l'indirizzo inizia con il metodo HTTP, come in AJAX()) e chiama il callback con la risposta interpretata. Il quarto argomento facoltativo riceve la percentuale mentre il caricamento è in corso. Costruisci il FormData da un input di file (new FormData() e append('file', input.files[0])); il lato server è la route @upload della ricetta sull'upload di file.
exports.install = function() {
ROUTE('POST /upload/ @upload <5mb', upload);
};
function upload($) {
var file = $.files[0];
file.read(function(err, buffer) {
$.json({ filename: file.filename, text: buffer.toString('utf8'), title: $.body.title });
});
}<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>jComponent</title>
<link rel="stylesheet" href="//cdn.componentator.com/spa.min@20.css" />
<script src="//cdn.componentator.com/spa.min@20.js"></script>
</head>
<body>
<script>
var form = new FormData();
form.append('title', 'My note');
form.append('file', new Blob(['hello from the browser'], { type: 'text/plain' }), 'note.txt');
var progress = [];
UPLOAD('POST /upload/', form, function(response) {
SET('uploaded', response);
}, function(percentage) {
progress.push(percentage);
});
</script>
</body>
</html> Provalo nel tuo browser
Modifica l'esempio ed eseguilo. Gira in un frame isolato del tuo browser; nulla viene inviato al nostro server.
Anteprima