Skip to content

Commit 973fd56

Browse files
File attachment
1 parent c5f316f commit 973fd56

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "uquik",
3-
"version": "1.0.22",
3+
"version": "1.0.23",
44
"description": "uQuik HTTP(S) framework",
55
"main": "index.js",
66
"scripts": {

src/Response.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// eslint-disable-next-line no-unused-vars
22
const { Readable, Writable } = require('readable-stream')
3+
const path = require('path')
34
// eslint-disable-next-line no-unused-vars
45
const Server = require('./Server') // lgtm [js/unused-local-variable]
56
const cookie = require('./helpers/cookie')
@@ -461,6 +462,23 @@ class Response extends Writable {
461462
}
462463
}
463464

465+
/**
466+
* Writes approriate headers to signify that file at path has been attached.
467+
*
468+
* @param {String} path
469+
* @param {String=} name
470+
* @returns {Response}
471+
*/
472+
attachment (filePath, name) {
473+
if (filePath) {
474+
const fileName = name || path.basename(filePath)
475+
this.header('Content-Disposition', `attachment; filename="${fileName}"`).type(path.extname(fileName))
476+
} else {
477+
this.header('Content-Disposition', 'attachment')
478+
}
479+
return this
480+
}
481+
464482
/**
465483
* Instantly aborts/closes current request without writing a status response code.
466484
* Use this only in extreme situations to abort a request where a proper response is not neccessary.

0 commit comments

Comments
 (0)