Skip to content

Instantly share code, notes, and snippets.

@scottgwald
Last active May 17, 2016 00:31
Show Gist options
  • Save scottgwald/3a55b3fae49e408a0b4084895b6710ea to your computer and use it in GitHub Desktop.
Save scottgwald/3a55b3fae49e408a0b4084895b6710ea to your computer and use it in GitHub Desktop.

Usage

  1. npm install

  2. node app.js

  3. In browser open url "http://localhost:8000"

  4. Select file to upload.

  5. Click upload.

  6. The file is saved as "filename.jpg."

var express = require('express');
var fileUpload = require('express-fileupload');
var app = express();
// default options
app.use(fileUpload());
app.use(express.static('.'));
app.post('/upload', function(req, res) {
var sampleFile;
if (!req.files) {
res.send('No files were uploaded.');
return;
}
sampleFile = req.files.sampleFile;
sampleFile.mv('./filename.jpg', function(err) {
if (err) {
res.status(500).send(err);
}
else {
res.send('File uploaded!');
}
});
});
app.listen(8000, function () {
console.log('Example app listening on port 8000!');
});
<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>
{
"dependencies" : {
"express": "latest",
"express-fileupload": "latest"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment