ForgeTotal.js 5
en

Total.js 5

Receive file uploads

Total.js 5 · total5@0.0.19 routinguploadfiles Executed against Total.js 5

Mark the route with @upload so it only accepts multipart/form-data, and set a size limit that fits the files you expect (here <5mb). The uploaded files are in $.files; the other fields of the form are in $.body. Each file has name, filename, size and type, and methods such as read(), copy() and move().

controllers/upload.js
exports.install = function() {
	ROUTE('POST /upload/ @upload <5mb', upload);
};

function upload($) {

	var file = $.files[0];

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

	file.read(function(err, buffer) {
		$.json({
			filename: file.filename,
			size: file.size,
			type: file.type,
			text: buffer.toString('utf8'),
			title: $.body.title
		});
	});
}