extractFilename
Extract a filename from HTTP response headers, parsing Content-Disposition with a fallback name for reliable file downloads.
Usage
extractFilename extracts a filename from HTTP response headers.
import { extractFilename } from '@movk/core'
// Extract filename from response headers
const headers = new Headers({
'content-disposition': 'attachment; filename="report.pdf"'
})
const filename = extractFilename(headers, 'download')
console.log(filename) // 'report.pdf'
// Handle encoded filenames
const encodedHeaders = new Headers({
'content-disposition': 'attachment; filename*=UTF-8\'\'%E6%8A%A5%E5%91%8A.pdf'
})
const encodedFilename = extractFilename(encodedHeaders)
console.log(encodedFilename) // '报告.pdf'
API
extractFilename(headers?, fallbackName?)
Extract a filename from HTTP response headers.
Parameters
headers
Headers
The response headers object.
fallbackName
string
The fallback filename, defaults to
'file'.Returns
returns
string
The extracted filename.
Changelog
No recent changes