express - 使用本机反应将图像上传到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'
})
}
});
})
解决方案
Heroku 文件系统是短暂的,您不能使用它在运行时持久存储任何内容。相反,您可以使用Heroku Elements 市场的插件来满足您的存储需求。对于您的特定用例,Cloudinary听起来可能是一个不错的选择。他们的免费计划为您提供 10 GB 的存储空间。您可以从您的 node.js 代码轻松地将图像上传到 cloudinary,如此处所述。
推荐阅读
- javascript - MongoDB 嵌套聚合查找
- typescript - 在 vuex-module-decorators 的 @ModuleAction 中使用状态
- javascript - 我如何设计这些卡片,以便在点击时打开指向新页面的链接?
- javascript - 在 Flutter 中使用 Html 和 Javascript
- splash-screen - 如何在 yocto 中加载用户空间之前显示引导屏幕
- apache-spark - 是否可以使用 mapgroupwithstate 作为滑动窗口
- prisma2 - 棱镜迁移缺少上下功能
- python - 在 Robot Framework 中为连接的所有查询设置架构
- javascript - index.js:1 警告:失败的道具类型:不推荐使用“ForwardRef(ButtonBase)”的道具“buttonRef”。改用`ref`
- firebase - Flutter GetX 变量发生变化,但 UI 没有