node.js - 无法使用 Node.js 和 Heroku 将文件上传到文件夹中

标签 node.js heroku multer

上传文件时遇到问题。我正在尝试使用 Node.js 将文件上传到文件夹中,我的应用程序部署在 Heroku 中。我在下面提供我的代码。

var multer  = require('multer')
var storage =multer.diskStorage({
  destination: function (req, file, callback) {
    callback(null, './uploads');
  },
  filename: function (req, file, callback) {
    callback(null, Date.now()+'-'+file.originalname);
  }
});
var upload = multer({ storage : storage });
app.post('/api/users/save-card-file',upload.single('file'), function (req, res, next) { 
   var data={'filename':res.req.file.filename};
   res.send(data);
})

在这里,我尝试将文件上传到 uploads 文件夹,它在 localhost 中运行时工作正常,但我正在将我的应用程序上传到 Heroku,响应即将到来,但是没有文件上传到 uploads 文件夹。在 Heroku 上上传文件后,我收到以下响应。

{
    "filename": "1503419364442-btechmarksheet.jpg"
}

但是 uploads 文件夹中没有文件。它是 localhost 中的工作文件,但在 Heroku 中我遇到了此类问题。

最佳答案

来自 https://devcenter.heroku.com/articles/dynos#ephemeral-filesystem :

Each dyno gets its own ephemeral filesystem, with a fresh copy of the most recently deployed code. During the dyno’s lifetime its running processes can use the filesystem as a temporary scratchpad, but no files that are written are visible to processes in any other dyno and any files written will be discarded the moment the dyno is stopped or restarted. For example, this occurs any time a dyno is replaced due to application deployment and approximately once a day as part of normal dyno management.

Heroku 的文件系统不能用作持久文件系统。 Dyno 在后续请求之间的空闲时间后重新启动,重新初始化文件系统,这就是即使上传成功,您稍后也无法找到文件的原因。

关于node.js - 无法使用 Node.js 和 Heroku 将文件上传到文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45822891/

相关文章:

javascript - PreventDefault() 方法不阻止页面重新加载 ReactJS

heroku - Redis 4.0 与 5.0 RDB 文件格式冲突 : `Can' t handle RDB format version 9`

javascript - Heroku 的配置操作电缆 - 错误 localhost :3000/cable

javascript - forEach 的简单 promise

mysql - Express View 引擎无法识别发送到前端的数据

laravel - Heroku 和 Laravel 护照

Node.js 类型错误 : Cannot read property 'path' of undefined

node.js - 使用node.js、needle、busboy/multer将文件从一台服务器发送到另一台服务器

javascript - 我收到重复 key 错误 -- MongoError : E11000 duplicate key error collection. 我确实有 2 个集合、用户和个人资料。

node.js - 如何让 cli 运行在 io.js 上