Skip to content

Instantly share code, notes, and snippets.

@kmaher9
Created July 3, 2019 21:11
Show Gist options
  • Save kmaher9/5c2793b0c8751bbaa5fc8920fdb2f2dc to your computer and use it in GitHub Desktop.
Save kmaher9/5c2793b0c8751bbaa5fc8920fdb2f2dc to your computer and use it in GitHub Desktop.
const File = require('../models/file')
const fs = require('fs')
exports.getFile = async function (request, response) {
let file = null
try { file = await File.findById(request.params.id) } catch (err) {}
if (!file) {
return response.status(404).json({
"data": {
"error": "unable to find requested file"
}
})
}
var stat = fs.statSync(file.location)
response.writeHead(200, {
'Content-Type': file.mime,
'Content-Length': stat.size,
'Content-Disposition': `attachment;filename=${file.name}`
})
let stream = fs.createReadStream(file.location)
stream.pipe(response)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment