# Carregar ficheiros com UPLOAD() e progresso

> Envie um FormData para uma rota @upload do Total.js com UPLOAD() e acompanhe o progresso.

Framework: jcomponent20
Product: jComponent 20 (jcomponent@20)
Language: pt
Content-SHA256: b6d2009f18d323728b29abc9a54f8274e87afca4d62362046d6f5203a052d7a6
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: jcomponent20@3fedd009a277f50fddc608b97afbec553b67c51b
Review: not-recorded

`UPLOAD('POST /url/', formdata, callback, onprogress)` envia um `FormData` (o endereço começa com o método HTTP, como em `AJAX()`) e chama o callback com a resposta interpretada. O quarto argumento opcional recebe a percentagem enquanto o carregamento decorre. Construa o `FormData` a partir de um input de ficheiro (`new FormData()` e `append('file', input.files[0])`); o lado do servidor é a rota `@upload` da receita sobre carregamento de ficheiros.

```js controllers/upload.js
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 });
	});
}
```

```html public/index.html
<!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>
```
