Skip to content

Instantly share code, notes, and snippets.

@rupertqin
Last active August 29, 2015 14:07
Show Gist options
  • Save rupertqin/45a9d17e0393a7312bfa to your computer and use it in GitHub Desktop.
Save rupertqin/45a9d17e0393a7312bfa to your computer and use it in GitHub Desktop.
upload image in node
// express@3.2
// http://tonyspiro.com/uploading-and-resizing-an-image-using-node-js/
var express = require("express");
var app = express()
/// Include the express body parser
app.configure(function () {
app.use(express.bodyParser());
});
var form = "<!DOCTYPE HTML><html><body>" +
"<form method='post' action='/upload' enctype='multipart/form-data'>" +
"<input type='file' name='image'/>" +
"<input type='submit' /></form>" +
"</body></html>";
app.get('/', function (req, res){
res.writeHead(200, {'Content-Type': 'text/html' });
res.end(form);
});
/// Include the node file module
var fs = require('fs');
/// Include ImageMagick
var im = require('imagemagick');
/// Post files
app.post('/upload', function(req, res) {
fs.readFile(req.files.image.path, function (err, data) {
var imageName = req.files.image.name
/// If there's an error
if(!imageName){
console.log("There was an error")
res.redirect("/");
res.end();
} else {
var newPath = __dirname + "/uploads/fullsize/" + imageName;
var thumbPath = __dirname + "/uploads/thumbs/" + imageName;
/// write file to uploads/fullsize folder
fs.writeFile(newPath, data, function (err) {
/// write file to uploads/thumbs folder
im.resize({
srcPath: newPath,
dstPath: thumbPath,
width: 200
}, function(err, stdout, stderr){
if (err) throw err;
console.log('resized image to fit within 200x200px');
});
res.redirect("/uploads/fullsize/" + imageName);
});
}
});
});
/// Show files
app.get('/uploads/fullsize/:file', function (req, res){
file = req.params.file;
var img = fs.readFileSync(__dirname + "/uploads/fullsize/" + file);
res.writeHead(200, {'Content-Type': 'image/jpg' });
res.end(img, 'binary');
});
app.get('/uploads/thumbs/:file', function (req, res){
file = req.params.file;
var img = fs.readFileSync(__dirname + "/uploads/thumbs/" + file);
res.writeHead(200, {'Content-Type': 'image/jpg' });
res.end(img, 'binary');
});
app.listen(8080)
//http://tonyspiro.com/uploading-resizing-images-fly-node-js-express/
//npm install express
//npm install formidable
//npm install quickthumb
//npm install fs-extra
var express = require("express"),
app = express(),
formidable = require('formidable'),
util = require('util')
fs = require('fs-extra'),
qt = require('quickthumb');
// Use quickthumb
app.use(qt.static(__dirname + '/'));
app.post('/upload', function (req, res){
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
res.end(util.inspect({fields: fields, files: files}));
});
form.on('end', function(fields, files) {
/* Temporary location of our uploaded file */
var temp_path = this.openedFiles[0].path;
/* The file name of the uploaded file */
var file_name = this.openedFiles[0].name;
/* Location where we want to copy the uploaded file */
var new_location = 'uploads/';
fs.copy(temp_path, new_location + file_name, function(err) {
if (err) {
console.error(err);
} else {
console.log("success!")
}
});
});
});
// Show the upload form
app.get('/', function (req, res){
res.writeHead(200, {'Content-Type': 'text/html' });
/* Display the file upload form. */
var form = '<form action="/upload" enctype="multipart/form-data" method="post"><input name="title" type="text" /><input multiple="multiple" name="upload" type="file" /><input type="submit" value="Upload" /></form>';
res.end(form);
});
app.listen(8080);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment