# Sube ficheros con UPLOAD() y progreso

> Envía un FormData a una ruta @upload de Total.js con UPLOAD() y sigue el progreso.

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

`UPLOAD('POST /url/', formdata, callback, onprogress)` envía un `FormData` (la dirección empieza con el método HTTP, como en `AJAX()`) y llama al callback con la respuesta interpretada. El cuarto argumento opcional recibe el porcentaje mientras se sube. Construye el `FormData` a partir de un input de fichero (`new FormData()` y `append('file', input.files[0])`); el lado servidor es la ruta `@upload` de la receta de subida de ficheros.

```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>
```
