Route-
app.post('/add-product',(req,res)=>{
var image=randomstring.generate({
length: 12
});
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
var oldpath = files.pimage.path;
var newpath = __dirname;
var path=newpath.split('controllers');
path=path[0]+'/public/productimages/'+image+".jpg";
fs.readFile(oldpath, function (err, data) {
if (err) throw err;
// Write the file
fs.writeFile(path, data, function (err) {
if (err) throw err;
});
});
});
});
Got some problem with uploading images to Heroku server, on Localhost everything work great. But on Heroku when I upload image, my app is getting crashed on these lines.
fs.writeFile(path, data, function (err) {
if (err) throw err;
});
Heroku log screenshot:
After changing '/public.....' to 'public/productimages/....'
I am getting : no such file or directory, open '/app/public/productimages/8sRueNlG2k3F.jpg'
These errors are typically caused by the target directory (in your case, /app/public/productimages/
) not yet existing.
You can use a package like mkdirp
to create the directory when your app starts, or you could create the directory locally and create an empty placeholder file (named .placeholder
, for instance) that you commit to your Git repository (because Git only deals with files, not directories, you can't add a completely empty directory to a repository).