decode filename when match an urlencode filename from contentDispositionFilename

This commit is contained in:
wangsongyan 2019-07-30 11:37:10 +08:00
parent 323b2eabcf
commit c61205d980
2 changed files with 19 additions and 0 deletions

View File

@ -61,6 +61,11 @@ function extractFilenameFromHeader(getResponseHeader) {
const contentDisposition = getResponseHeader('Content-Disposition');
if (contentDisposition) {
let filename = getFilenameFromContentDispositionHeader(contentDisposition);
if (filename.includes('%')) {
try {
filename = decodeURIComponent(filename);
} catch (ex) {}
}
if (/\.pdf$/i.test(filename)) {
return filename;
}

View File

@ -238,6 +238,20 @@ describe('network_utils', function() {
}
throw new Error(`Unexpected headerName: ${headerName}`);
})).toEqual('filename.pdf');
expect(extractFilenameFromHeader((headerName) => {
if (headerName === 'Content-Disposition') {
return 'attachment; filename="%e4%b8%ad%e6%96%87.pdf"';
}
throw new Error(`Unexpected headerName: ${headerName}`);
})).toEqual('中文.pdf');
expect(extractFilenameFromHeader((headerName) => {
if (headerName === 'Content-Disposition') {
return 'attachment; filename="100%.pdf"';
}
throw new Error(`Unexpected headerName: ${headerName}`);
})).toEqual('100%.pdf');
});
it('gets the filename from the response header (RFC 6266)', function() {