How return file from service? #2968
Unanswered
ruslan-gennadievich
asked this question in
Q&A
Replies: 3 comments 2 replies
-
This would be done in a custom service middleware: file.service.jsconst { File } = require('./file.class');
const hooks = require('./file.hooks');
module.exports = function (app) {
const options = {};
app.use(app.get('apiPrefix') + '/files', new File(options, app), (req, res, next) => {
const result = res.data
const fileStream = fs.createReadStream(result.file)
res.header('Content-Disposition', `attachment; filename="${path.basename(file)}"`)
res.header('Content-Type', 'application/octet-stream')
fileStream.pipe(res)
});
const service = app.service(app.get('apiPrefix') + '/files');
service.hooks(hooks);
}; file.class.jsconst fs = require('fs')
const path = require('path')
const FMANAGERDIR = './public/'
exports.File = class Files {
async get(file, params) {
const currentDirPath = FMANAGERDIR
return {
file: currentDirPath + '/' + file
}
}
}; |
Beta Was this translation helpful? Give feedback.
0 replies
-
What about v4 branch? |
Beta Was this translation helpful? Give feedback.
1 reply
-
How to do that in v5 now? |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Hi
I need Link to download some file. This link is 'get' method in service. I'm try some hacks in service file:
file.service.js
file.class.js
But this not working, What is true way for resolve this task?
Beta Was this translation helpful? Give feedback.
All reactions