首页 > 解决方案 > 使用本机反应将图像上传到heroku

问题描述

我正在尝试将图像从 react native 上传到 public/images/ 文件夹中的 heroku express 应用程序。它向我显示了成功消息,但图像文件夹中没有图像。同时,当我在 localhost 上尝试此代码时,效果很好..

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'
            })
        }
    });
})

标签: expressreact-nativeheroku

解决方案


Heroku 文件系统是短暂的,您不能使用它在运行时持久存储任何内容。相反,您可以使用Heroku Elements 市场的插件来满足您的存储需求。对于您的特定用例,Cloudinary听起来可能是一个不错的选择。他们的免费计划为您提供 10 GB 的存储空间。您可以从您的 node.js 代码轻松地将图像上传到 cloudinary,如此处所述


推荐阅读