0

I copied a code from the net that upload files and I have accidentally deleted something but I don't remember what it was, my code is as follows :

var multer = require('multer');
var storage = multer.memoryStorage();
var upload = multer({ storage: storage });

router.post('/upload', upload.array('files'), function (req, res, next) {

var uploadPromises = req.files.map(function (file) {

});

return Promise.all(uploadPromises);
});

Can someone please try to figure out what was inside uploadPromises function in order to have my upload worked

Youssef Korchi
  • 127
  • 1
  • 4
  • 14
  • Couldn't you find it in the net again ? – TMG May 30 '16 at 16:11
  • no , I have been looking for 2 days but to no avail – Youssef Korchi May 30 '16 at 16:13
  • Begin with [`return new Promise(function (resolve, reject) { ... });`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise). Beyond that, it depends on what you want done with the files, but call either `resolve()` (for success) or `reject()` (for an error). – Jonathan Lonowski May 30 '16 at 16:14
  • _"Can someone please try to figure out what was inside uploadPromises function in order to have my upload worked"_ Tried `return file`? – guest271314 May 30 '16 at 16:20
  • I just want to store it in my /uploads directory , I can read my file using the argument 'file' from my function but it's not uploaded – Youssef Korchi May 30 '16 at 16:22
  • Try `return Promise.resolve(file)` – guest271314 May 30 '16 at 16:24
  • I tried multer's DiskStorage (destination: function (req, file, cb) { cb(null, '/uploads/') }) and I get that Error: ENOENT: no such file or directory, open '/uploads/2413fbf2ba2379ed149d3d5f4fb1ca02' – Youssef Korchi May 30 '16 at 16:34
  • `MemoryStorage` adds a [`buffer` to each `file` object](https://github.com/expressjs/multer#api) that you can [write to a file on disk](https://stackoverflow.com/questions/2496710/writing-files-in-node-js). Note that you'll have to determine the full path to the `uploads` directory. The file system module doesn't work with URL paths. (For example, if `uploads` is being served by `express.static(__dirname + '/public')`, then it's at `__dirname + '/public/uploads'`). – Jonathan Lonowski May 30 '16 at 16:34

0 Answers0