ctx.attachment support no-ascii filename
This commit is contained in:
parent
a462876b25
commit
cca2438f64
3 changed files with 30 additions and 4 deletions
|
@ -15,6 +15,7 @@ var assert = require('assert');
|
||||||
var http = require('http');
|
var http = require('http');
|
||||||
var path = require('path');
|
var path = require('path');
|
||||||
var vary = require('vary');
|
var vary = require('vary');
|
||||||
|
var contentDisposition = require('content-disposition');
|
||||||
var basename = path.basename;
|
var basename = path.basename;
|
||||||
var extname = path.extname;
|
var extname = path.extname;
|
||||||
|
|
||||||
|
@ -245,9 +246,7 @@ module.exports = {
|
||||||
|
|
||||||
attachment: function(filename){
|
attachment: function(filename){
|
||||||
if (filename) this.type = extname(filename);
|
if (filename) this.type = extname(filename);
|
||||||
this.set('Content-Disposition', filename
|
this.set('Content-Disposition', contentDisposition(filename));
|
||||||
? 'attachment; filename="' + basename(filename) + '"'
|
|
||||||
: 'attachment');
|
|
||||||
},
|
},
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -26,6 +26,7 @@
|
||||||
"media-typer": "~0.3.0",
|
"media-typer": "~0.3.0",
|
||||||
"on-finished": "~2.1.0",
|
"on-finished": "~2.1.0",
|
||||||
"co": "~3.1.0",
|
"co": "~3.1.0",
|
||||||
|
"content-disposition": "~0.1.0",
|
||||||
"debug": "*",
|
"debug": "*",
|
||||||
"fresh": "~0.2.1",
|
"fresh": "~0.2.1",
|
||||||
"koa-compose": "~2.3.0",
|
"koa-compose": "~2.3.0",
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
|
|
||||||
|
var request = require('supertest');
|
||||||
var context = require('../context');
|
var context = require('../context');
|
||||||
|
var koa = require('../..');
|
||||||
|
|
||||||
describe('ctx.attachment([filename])', function(){
|
describe('ctx.attachment([filename])', function(){
|
||||||
describe('when given a filename', function(){
|
describe('when given a filename', function(){
|
||||||
|
@ -18,4 +20,28 @@ describe('ctx.attachment([filename])', function(){
|
||||||
ctx.response.header['content-disposition'].should.equal('attachment');
|
ctx.response.header['content-disposition'].should.equal('attachment');
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
describe('when given a no-ascii filename', function(){
|
||||||
|
it('should set the encodeURI filename param', function(){
|
||||||
|
var ctx = context();
|
||||||
|
ctx.attachment('path/to/include-no-ascii-char-中文名-ok.png');
|
||||||
|
var str = 'attachment; filename=\"include-no-ascii-char-???-ok.png\"; filename*=UTF-8\'\'include-no-ascii-char-%E4%B8%AD%E6%96%87%E5%90%8D-ok.png';
|
||||||
|
ctx.response.header['content-disposition'].should.equal(str);
|
||||||
|
})
|
||||||
|
|
||||||
|
it('should work with http client', function(done){
|
||||||
|
var app = koa();
|
||||||
|
|
||||||
|
app.use(function* (next){
|
||||||
|
this.attachment('path/to/include-no-ascii-char-中文名-ok.json')
|
||||||
|
this.body = {foo: 'bar'}
|
||||||
|
})
|
||||||
|
|
||||||
|
request(app.listen())
|
||||||
|
.get('/')
|
||||||
|
.expect('content-disposition', 'attachment; filename="include-no-ascii-char-???-ok.json"; filename*=UTF-8\'\'include-no-ascii-char-%E4%B8%AD%E6%96%87%E5%90%8D-ok.json')
|
||||||
|
.expect({foo: 'bar'})
|
||||||
|
.expect(200, done)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
Loading…
Reference in a new issue