Skip to content

Instantly share code, notes, and snippets.

@neomadara
Forked from joemccann/up.js
Created January 25, 2016 01:58
Show Gist options
  • Save neomadara/ffbe3d41789f873671fe to your computer and use it in GitHub Desktop.
Save neomadara/ffbe3d41789f873671fe 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