Skip to content

Instantly share code, notes, and snippets.

@Snack-X
Last active December 17, 2015 18:19
Show Gist options
  • Save Snack-X/5652635 to your computer and use it in GitHub Desktop.
Save Snack-X/5652635 to your computer and use it in GitHub Desktop.
Simple file upload script written in node.js. Requires node-formidable.
var formidable = require("formidable"),
http = require("http"),
util = require("util"),
fs = require("fs");
http.createServer(function(req, res) {
if(req.url == "/upload" && req.method.toLowerCase() == "post") {
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
time = new Date();
name = files.upload.name.split(".");
extension = name[name.length-1];
image_url = time.getFullYear()+"-"+(time.getMonth()+1)+"-"+time.getDate()+"_"+time.getHours()+"-"+time.getMinutes()+"-"+time.getSeconds()+"."+extension;
fs.renameSync(files.upload.path, "/home/web/image/"+image_url);
res.writeHead(200, {"Content-Type": "text/plain"});
res.end("http://i.korsnack.kr/"+image_url);
});
return;
}
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/upload" enctype="multipart/form-data" method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>'
);
}).listen(2001);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment