-
-
Save neomadara/ffbe3d41789f873671fe to your computer and use it in GitHub Desktop.
upload express example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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