0

I try to download an uploaded file from a data folder, OUT of the asset folder in a Sails.js 0.12 app.

I ended up installing Skipper-disk npm to do so with :

var SkipperDisk = require('skipper-disk');
var fileAdapter = SkipperDisk(/* optional opts */);

res.attachment('./data/'+req.param('product_id')+ '/' + req.param('file'));
fileAdapter.read('./data/'+req.param('product_id')+ '/' + req.param('file'))
.on('error', function (err){
    return res.serverError(err);
})
.pipe(res);

I'm a bit disappointed about it because I wanted to find a way to do it without installing more dependencies (skipper-disk). I tried things like : sailsjs send a file on the fly with res.attachment(), How do I authenticate access to assets folder in sails.js, Uploading multiple files with Sails.js 0.10 and Skipper using Dropzone.js, Sails.js Download File To Client, and many more, but nothing else work.

Is there another way than skipper-disk to make the upload work ?

Also, I needed more than one file downloaded with the previous controller action, but my request from the front end only opens the first file selected.

Here's that code :

files.forEach(function(file) {
    window.open('/product/' + id + '/download/' + file, '_blank');
});

Is there a way to get multiple files from the back end ?

StS
  • 49
  • 1
  • 11
  • Have you try out the skipper itself beside skipper-disk? I guess the default folder is: .tmp/uploads/ and ensure that your path is at the correct location path. Also try this: http://blog.tunaxor.me/2017/01/07/upload-download-sails/ – Kyros Koh Jan 16 '19 at 04:11
  • To upload files I use : `uploadFile.upload({ dirname: '../../data/'+req.session.selectedProduct.id, saveAs: req.file('file')._files[0].stream.filename }, function onUploadComplete(err, files) { ... }` and it works, and I think it is skipper ? – StS Jan 16 '19 at 13:20

1 Answers1

0

Well for my first question, I'm not sure if it's the best way to do it, but this works fine without Skipper-disk (the server on which the app is running is a pain when I need to install a new npm, so I try to do without !) :

const Path = require('path');
const fs = require('fs');

fs.createReadStream(Path.resolve('./data/'+req.param('product_id')+ '/' + req.param('file')))
.on('error', function (err) {
    return res.serverError(err);
})
.pipe(res);
StS
  • 49
  • 1
  • 11