Skip to content

Commit 393d9d8

Browse files
committed
response header check if it exists for binary responses before extracting the filename from it
1 parent f6fcdcf commit 393d9d8

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

src/components/api-request.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,14 +1575,16 @@ export default class ApiRequest extends LitElement {
15751575
if (this.responseIsBlob) {
15761576
const contentDisposition = fetchResponse.headers.get('content-disposition') || '';
15771577
let filenameFromContentDeposition = 'filename';
1578-
const filenameStarRegexMatch = contentDisposition.match(/filename\*=\s*UTF-8''([^;]+)/); // Support Headers like >>> Content-Disposition: attachment; filename*=UTF-8''example%20file.pdf
1579-
if (filenameStarRegexMatch) {
1580-
filenameFromContentDeposition = decodeURIComponent(filenameStarRegexMatch[1]); // the filename* format in the Content-Disposition header follows RFC 5987, which allows encoding non-ASCII characters using percent encoding. so example%20file.pdf becomes example file.pdf
1581-
} else {
1582-
// Fallback to the regular filename format
1583-
const filenameMatch = contentDisposition.match(/filename="?([^"]+)"?/); // Content-Disposition: attachment; filename=example.pdf
1584-
if (filenameMatch) {
1585-
filenameFromContentDeposition = filenameMatch[1];
1578+
if (contentDisposition) {
1579+
const filenameStarRegexMatch = contentDisposition.match(/filename\*=\s*UTF-8''([^;]+)/); // Support Headers like >>> Content-Disposition: attachment; filename*=UTF-8''example%20file.pdf
1580+
if (filenameStarRegexMatch) {
1581+
filenameFromContentDeposition = decodeURIComponent(filenameStarRegexMatch[1]); // the filename* format in the Content-Disposition header follows RFC 5987, which allows encoding non-ASCII characters using percent encoding. so example%20file.pdf becomes example file.pdf
1582+
} else {
1583+
// Fallback to the regular filename format
1584+
const filenameMatch = contentDisposition.match(/filename="?([^"]+)"?/); // Content-Disposition: attachment; filename=example.pdf
1585+
if (filenameMatch) {
1586+
filenameFromContentDeposition = filenameMatch[1];
1587+
}
15861588
}
15871589
}
15881590
this.respContentDisposition = filenameFromContentDeposition;

0 commit comments

Comments
 (0)