Created
July 23, 2015 05:12
-
-
Save hartzis/fb43721affdf9acd8555 to your computer and use it in GitHub Desktop.
Handle Express Image File Upload
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
let multiparty = require('multiparty'); | |
let fs = require('fs'); | |
function saveImage(req, res) { | |
let form = new multiparty.Form(); | |
form.parse(req, (err, fields, files) => { | |
let {path: tempPath, originalFilename} = files.imageFile[0]; | |
let copyToPath = "./images/" + originalFilename; | |
fs.readFile(tempPath, (err, data) => { | |
// make copy of image to new location | |
fs.writeFile(newPath, data, (err) => { | |
// delete temp image | |
fs.unlink(tmpPath, () => { | |
res.send("File uploaded to: " + newPath); | |
}); | |
}); | |
}); | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello thank you for your code , i have an issue :
the goal is to insert the uploadedfile into json object