Skip to content

Latest commit

 

History

History
76 lines (60 loc) · 1.67 KB

README.md

File metadata and controls

76 lines (60 loc) · 1.67 KB

express-fileupload

Simple express file upload middleware that wraps around connect-busboy.

Install

npm install express-fileupload

Important Note

Add app.use(fileUpload()) AFTER app.use(bodyParser.json) and any other bodyParser middlewares! This limitation will be explored and resolved in an upcoming release.

Options

Pass in Busboy options directly to express-fileupload (using Busboy v0.2.13). Check out the Busboy documentation here: https://github.com/mscdex/busboy#api

app.use(fileUpload({
	limits: { fileSize: 50 * 1024 * 1024 },
}));

Example

JavaScript

var express = require('express');
var fileUpload = require('express-fileupload');
var app = express();

// default options
app.use(fileUpload());

app.post('/upload', function(req, res) {
	var sampleFile;

	if (!req.files) {
		res.send('No files were uploaded.');
		return;
	}

	sampleFile = req.files.sampleFile;
	sampleFile.mv('/somewhere/on/your/server/filename.jpg', function(err) {
		if (err) {
			res.status(500).send(err);
		}
		else {
			res.send('File uploaded!');
		}
	});
});

Form

<html>
	<body>
		<form ref='uploadForm' 
			id='uploadForm' 
			action='http://localhost:8000/upload' 
			method='post' 
			encType="multipart/form-data">
				<input type="file" name="sampleFile" />
				<input type='submit' value='Upload!' />
		</form>		
	</body>
</html>

Thanks & Credit