2017-11-14 6 views
1

私はこのようなファイルのダウンロードを提供するためにExpressを使用しています:を使用する特殊文字(ウムラウト)エクスプレス応答で

const fileStream = Storage.getFileStream(path); 
res.setHeader('Content-disposition', `attachment; filename="${filename}"`); 
res.setHeader('Content-type', contentType); 
res.setHeader('Content-length', size); 
fileStream.pipe(res); 

filenameは、特殊文字(A、U、E、...)エラーが含まれている場合私は許可されていない文字を使用していると言って投げられます。

unidecodeを使用して解決策が見つかりました。しかし、それを "A"に変換する代わりに "Ä"を保持する解決策があります

私はこの問題を検索しますが、これを正しく処理する方法は非常に混乱しています。 (後で追加)...


ソリューション:私は私のために動作し、ファイル名にすべての特殊文字を保持し、この解決策を見つけた:

const filename = encodeURI(file.filename); 
res.setHeader('Content-disposition', `attachment; filename*=UTF-8''${filename}; filename=${filename}`); 
res.setHeader('Content-type', contentType); 
res.setHeader('Content-length', size); 

答えて

0

あなたはのいずれかの文字列を正規化してみて偉大な図書館 JavaScript Unicode 8.0正規化 - NFC、NFD、NFKC、NFKD。私はそれが動作を期待https://github.com/walling/unorm

var unorm = require('unorm'); 

var text = 
    'The \u212B symbol invented by A. J. \u00C5ngstr\u00F6m ' + 
    '(1814, L\u00F6gd\u00F6, \u2013 1874) denotes the length ' + 
    '10\u207B\u00B9\u2070 m.'; 

var combining = /[\u0300-\u036F]/g; // Use XRegExp('\\p{M}', 'g'); see 
example.js. 

console.log('Regular: ' + text); 
console.log('NFC:  ' + unorm.nfc(text)); 
console.log('NFD:  ' + unorm.nfd(text)); 
console.log('NFKC:  ' + unorm.nfkc(text)); 
console.log('NFKD: * ' + unorm.nfkd(text).replace(combining, '')); 
console.log(' * = Combining characters removed from decomposed form.'); 

ソース -

NPM UNORM

サンプルコードをインストールする - は:使用して役に立つことができる 。