# Store uploaded files with FILESTORAGE

> Save uploads in a managed file storage with file.fs() and send them back with $.filefs().

Framework: total5
Product: Total.js 5 (total5@0.0.19)
Language: en
Content-SHA256: 6cff4aa6f8c4f6223f79ebf823911d6bce9231c2d2cab8e006db84ea9817ae7b
Snapshot: c0700e321579725c5cf4f5ace1ad7d2866d1b4f93c24a63c0b41b6b7998e976c
Sources: total5-installed@total5@0.0.19
Review: not-recorded

`FILESTORAGE(name)` is a file store: files are saved under an id (use `UID()`) and served back by that id. Receive the upload with `@upload`, save it with `file.fs(storage, id, callback)` and answer downloads with `$.filefs(storage, id)`, which sends the stored file with its `content-type`.

The storage name comes from the config file, so it can change per environment.

```text config
fs : files
```

```js controllers/files.js
exports.install = function() {
	ROUTE('POST /upload/ @upload <5mb', upload);
	ROUTE('FILE /downloads/*.*', download);
};

function upload($) {

	var file = $.files[0];

	if (!file) {
		$.invalid('No file');
		return;
	}

	var id = UID();

	file.fs(CONF.fs, id, function(err) {
		if (err)
			$.invalid(err);
		else
			$.json({ id: id, url: '/downloads/' + id + '.' + file.extension, filename: file.filename, size: file.size });
	});
}

function download($) {
	// /downloads/<id>.<extension>
	var name = $.split[1];
	var id = name.substring(0, name.lastIndexOf('.'));
	$.filefs(CONF.fs, id);
}
```
