3

I have an issue while uploading file. I am trying to upload file into folder using Node.js and my app is deployed in Heroku. I am providing my code below.

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

Here I am trying to upload file into uploads folder and it's working fine while running in localhost but I am uploading my App into Heroku and there the response is coming but no file uploaded into uploads folder. I am getting the below response after uploading the file on Heroku.

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

But no file is present inside uploads folder. It's working file in localhost but in Heroku I am getting this type of issue.

1 Answer 1

4

From 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's filesystem CAN NOT be used as a persistent filesystem. Dyno restarts after some idle time between subsequent requests, re-initializing the filesystem and this is the reason even when the upload succeeds, you're not able to find the file later.

7
  • what is the solution for this ? Can you please help me.
    – satya
    Aug 23, 2017 at 4:28
  • 2
    Save the file using a 3rd party service like AWS S3, or if the files are images, imgur and cloudinary are good services.
    – tbking
    Aug 23, 2017 at 6:40
  • I am saving images . So there is no way to save the file into folder inside heroku right ?
    – satya
    Aug 23, 2017 at 6:43
  • Yes, you need to use some service. Can't save it in a folder in Heroku.
    – tbking
    Aug 23, 2017 at 6:45
  • Actually I am using Node.js so which one will be good service provider.
    – satya
    Aug 23, 2017 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.