nodejs下载文件文件名乱码问题 1. [代码] 文件名乱码问题 var userAgent = (req.headers['user-agent']||'').toLowerCase(); if(userAgent.indexOf('msie') = 0 || userAgent.indexOf('chrome') = 0) { res.setHeader('Content-Dispositi
1. [代码]文件名乱码问题
var userAgent = (req.headers['user-agent']||'').toLowerCase(); if(userAgent.indexOf('msie') >= 0 || userAgent.indexOf('chrome') >= 0) { res.setHeader('Content-Disposition', 'attachment; filename=' + encodeURIComponent(fileName)); } else if(userAgent.indexOf('firefox') >= 0) { res.setHeader('Content-Disposition', 'attachment; filename*="utf8\'\'' + encodeURIComponent(fileName)+'"'); } else { /* safari等其他非主流浏览器只能自求多福了 */ res.setHeader('Content-Disposition', 'attachment; filename=' + new Buffer(fileName).toString('binary')); } res.setHeader('Content-Type', 'application/octet-stream;charset=utf8'); var opt = { flags: 'r' }; var stream = fs.createReadStream(Path, opt); stream.pipe(res); stream.on('close', function () { res.end(); });