问题
i am trying to upload image from react native to heroku express app in public/images/ folder. It showing me the success message but there is no image in the images folder. Meanwhile when i try this code on localhost, that works fine..
router.post('/add', (req, res) => {
let imageFile = req.files.photo;
let fileName = Date.now();
let imgName = fileName+req.files.photo.name;
let dirName = __dirname.replace('routes','public')
dirName = dirName+'/images/';
let path = dirName+imgName;
imageFile.mv(path, (err)=> {
if (err) {
return res.status(501).json({
message: 'error'
});
} else {
return res.status(200).json({
message: 'success'
})
}
});
})
回答1:
The Heroku file system is ephemeral, and you cannot use it to persistently store anything at run time. Instead, you can use an add-on from the Heroku Elements marketplace to implement your storage needs. For your specific use case, Cloudinary sounds like it might be a good match. Their Free plan gives you 10 GB of storage. You can easily upload an image to cloudinary from your node.js code, as described here.
来源:https://stackoverflow.com/questions/53098912/upload-image-to-heroku-using-react-native