Upload image to heroku using react native

时光怂恿深爱的人放手 提交于 2021-01-28 08:00:57

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!