使用react native将图像上传到heroku [英] Upload image to heroku using react native

查看:57
本文介绍了使用react native将图像上传到heroku的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

Heroku文件系统为 Heroku Elements市场中的附件来实现您的存储需求.对于您的特定用例, Cloudinary 听起来可能很合适.他们的免费计划为您提供10 GB的存储空间.您可以按照此处中的描述,轻松地从node.js代码将图像上传到cloudinary.

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.

这篇关于使用react native将图像上传到heroku的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆