Skip to content

Instantly share code, notes, and snippets.

@joemccann
Created November 18, 2011 16:02
Show Gist options
  • Save joemccann/1376855 to your computer and use it in GitHub Desktop.
Save joemccann/1376855 to your computer and use it in GitHub Desktop.
upload express example
app.post('/upload/photo', function(req, res, next){
var json_response = {}
req.form.complete(function (err, fields, files){
if (err){
next(err)
}
else{
// temporary filename
// console.dir(files)
var name = Object.keys(files)[0]
var path = files[name]['path']
var readableFilename = getFilenameNoSlash(path)
// usable/readable filename path
var readableFilenamePath = __dirname + "/public/img/" + getFilenameNoSlash(files[name]['filename']);
// this is the file located here on the server.
var localfilePath = __dirname + "/public/img/" + readableFilename
fs.rename(localfilePath, readableFilenamePath, function (err){
if(err) {
console.log(err)
json_response.message = "Failed to upload the image."
json_response.error = true
res.send(JSON.stringify(json_response), 200)
}else{
json_response.message = "Successfully uploaded the image."
json_response.error = false
res.send(JSON.stringify(json_response), 200)
// console.log('upload complete')
}
})
} // end else
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment